简体   繁体   English

使用BeginTransaction的SQLite.Net问题

[英]SQLite.Net Issue With BeginTransaction

I'm trying to use System.Data.Sqlite library, and I'm following the documentation about optimizing inserts so I copied this code directly out of the documentation: 我正在尝试使用System.Data.Sqlite库,我正在遵循有关优化插入的文档,因此我直接从文档中复制了此代码:

  using (SQLiteTransaction mytransaction = myconnection.BeginTransaction())
  {
    using (SQLiteCommand mycommand = new SQLiteCommand(myconnection))
    {
      SQLiteParameter myparam = new SQLiteParameter();
      int n;

      mycommand.CommandText = "INSERT INTO [MyTable] ([MyId]) VALUES(?)";
      mycommand.Parameters.Add(myparam);

      for (n = 0; n < 100000; n ++)
      {
        myparam.Value = n + 1;
        mycommand.ExecuteNonQuery();
      }
    }
    mytransaction.Commit();
  } 

Now, I initialize I connection right before that by using 现在,我通过使用来初始化我之前的连接

SqlConnection myconnection = new SqlConnection("Data Source=blah");

I have a Database named blah, with the correct tables and values. 我有一个名为blah的数据库,具有正确的表和值。

The problem is when I run this code, it says "Operation is not valid due to the current state of the object" 问题是当我运行这段代码时,它说“由于对象的当前状态,操作无效”

I've tried changing the code around several times, and it still points to BeginTransaction. 我已尝试多次更改代码,它仍指向BeginTransaction。 What gives? 是什么赋予了?

You may have declared and instantiated the connection, but have you opened it? 您可能已声明并实例化了该连接,但是您已打开它吗?

First thing I would try is to remove the transaction stuff, and see if the code actually works - see what that tells you... 我要尝试的第一件事就是删除交易内容,看看代码是否真的有效 - 看看它告诉你什么......

Martin 马丁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM