SQLite.Net 的 BeginTransaction 问题

发布于 2024-08-25 04:01:58 字数 848 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨离汐 2024-09-01 04:01:58

您可能已经声明并实例化了连接,但是您打开了它吗?

我要尝试的第一件事是删除交易内容,然后看看代码是否真正有效 - 看看它告诉你什么......

马丁

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文