SQLite.Net 的 BeginTransaction 问题
我正在尝试使用 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();
}
现在,我在此之前使用
SqlConnection myconnection = new SqlConnection("Data Source=blah");
I have a Database named 初始化连接等等,有正确的表格和值。
问题是当我运行这段代码时,它说“由于对象的当前状态,操作无效”
我尝试多次更改代码,但它仍然指向 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("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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能已经声明并实例化了连接,但是您打开了它吗?
我要尝试的第一件事是删除交易内容,然后看看代码是否真正有效 - 看看它告诉你什么......
马丁
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