错误:使用ExecuteNonQuery()时"无法在datareader处于活动状态时设置commandtext"

ali*_*hoo 6 .net c# sqlite system.data.sqlite

我监听数据流并将数据作为插入语句存储在a中,ConcurrentQueue并使用System.Threading.Timer间隔为1000的大量插入插入数据.整个场景在静态类上运行.这是代码:

static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    if (queryQueue.IsEmpty)
        return;
    string text = "";
//bulkBuilder is StringBuilder.
//queryQueue is ConcurrentQueue
    bulkBuilder.AppendLine("PRAGMA synchronous = 0;PRAGMA count_changes = FALSE;PRAGMA journal_mode=OFF;Begin;");
    while (queryQueue.TryDequeue(out text))
    {
        bulkBuilder.Append(text);
        bulkBuilder.AppendLine(";");
    }
    bulkBuilder.AppendLine("Commit;");

    try
    {
        sqlCommand.CommandText = bulkBuilder.ToString();
        sqlCommand.ExecuteNonQuery();
    }
    catch (System.Exception ex)
    {
        Console.WriteLine("Error while inserting Data : " + ex.Message);
    }
    finally
    {
        bulkBuilder.Clear();
    }

}
Run Code Online (Sandbox Code Playgroud)

有趣的是,sqlCommand仅用于插入,仅用ExecuteNonQuery()于此计时器.并且不时会收到错误消息"无法在datareader处于活动状态时设置commandtext".这完全是无稽之谈,因为这个代码无关与内心SQLiteDataReadersqlCommand.

我怎样才能摆脱这个错误?

Jon*_*eet 14

我会为每个SQL语句创建一个新的SqlCommand(或任何类型的sqlCommand).让连接池处理使其全部高效 - 每次您需要对数据库执行某些操作时:

  • 创建并打开连接
  • 创建命令
  • 执行命令
  • 处理命令和连接(带using语句)

这样,除了由于连接池空间不足等原因之外,您无法最终得到影响另一个命令的一个命令的本地状态.

开发人员经常尝试通过一次使用一个连接(和潜在的命令)进行优化,但这是一种虚假的经济,并导致像你所展示的那样的问题.