开发者

SQLite.Net Issue With BeginTransaction

开发者 https://www.devze.com 2022-12-23 03:08 出处:网络
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:

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:

  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("Da开发者_开发问答ta Source=blah");

I have a Database named blah, with the correct tables and values.

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. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号