SQLite.Net Issue With BeginTransaction

Posted by cam on Stack Overflow See other posts from Stack Overflow or by cam
Published on 2010-03-15T12:54:03Z Indexed on 2010/03/15 12:59 UTC
Read the original article Hit count: 319

Filed under:
|
|

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("Data 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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about sqlite