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:
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
精彩评论