views:

321

answers:

1

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?

+2  A: 

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

Martin Milan
I have some code where I haved used transactions with SQLite, but not on my works machine. If I remember, I'll have a look on my personal machine for you tonight...Martin.
Martin Milan
I'm an idiot, thanks. yeah, I had everything working before the transaction stuff, I guess I forgot I had to open the connection haha.
cam
No prob matey...
Martin Milan
sage advice. We've all been caught by this at some point.
Evildonald