多个插入事务连接超时- ADO.. NET - SQL服务器

本文关键字:ADO NET SQL 服务器 超时 插入 事务 连接 | 更新日期: 2023-09-27 17:54:37

我有一个包含多个插入的事务。所有的插入都很好,除了一个。我验证了参数,拼写,所有的一切,但似乎我还是没弄明白。它给了我错误:

Timeout expired. The timeout period elapsed prior to completion of the operation 
or the server is not responding.

我的交易是这样的:

SqlConnection db = new SqlConnection(connString);
DataSet dataSet = new DataSet();
SqlDataAdapter da = new SqlDataAdapter();
using (db)
{
    db.Open();
    SqlTransaction trans = db.BeginTransaction();
    try
    {
        //insert into COMMSignalDefinition !!Problem HERE
        da.InsertCommand = 
            new SqlCommand("INSERT INTO COMMSignalDefinition(Name) " 
                           + "VALUES (@name)", db, trans);
        da.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar);
        foreach (DataRow row in ds.Tables["COMMTerminalSignal"].Select())
        {
            da.InsertCommand.Parameters[0].Value = row.ItemArray[1];
            da.InsertCommand.ExecuteNonQuery();
        }
        // insert into COMMSignalExceptionDefinition -- names
        da.InsertCommand = 
            new SqlCommand("INSERT INTO COMMSignalExceptionDefinition(Name) " 
                           + "VALUES (@name)", db, trans);
        da.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar);
        foreach (DataRow row in ds.Tables["COMMSignalExceptionDef"].Select())
        {
            da.InsertCommand.Parameters[0].Value = row.ItemArray[1];
            da.InsertCommand.ExecuteNonQuery();
        }
        trans.Commit();
        MessageBox.Show("You have successfully imported your Settings. " 
                        + "You can now exit the program.", 
                         "Success",
                         MessageBoxButtons.OK, 
                         MessageBoxIcon.Information);
    }
    catch (Exception e)
    {
        trans.Rollback();
        MessageBox.Show(e.Message,
                        "Error", 
                        MessageBoxButtons.OK, 
                        MessageBoxIcon.Error);
    }
}

我有更多的插入工作良好(我删除了其余的),我在开始时移动了有问题的插入。我的问题是我能做错什么?我甚至用SQL Server Profiler,验证了"有问题的"查询是否被发送到服务器,结果确实如此!如果我在SQL Server Management studio中执行它,它也能工作。

Connection Timeout设置为30

你能给我一些线索吗?SQL Server版本是2005 !谢谢你!

多个插入事务连接超时- ADO.. NET - SQL服务器

经过几个小时的挖掘,我在Management Studio中做了一些测试,在那里我测试了一些根本没有提交的事务。所以它在等待一个提交,我一直在做,或者试图做插入…div !

如果这是一个相当长的运行操作,您可以尝试更改命令超时时间

  1. 检查连接字符串。也许你有一些错误在你的connString var
  2. 检查SqlDataAdapter命令超时。它们可能是导致异常的原因(参见http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx)。通常这种异常是长时间运行任务或未提交事务的结果。

连接超时时间设置为1

SqlConnection.ConnectionTimeout属性

等待连接打开的时间(以表示)。的默认值为15秒。

所以您已经将暂停设置为1秒。这样回答你的问题了吗?

备注:设置连接等待超时的时间连接中的ConnectTimeout或Connection Timeout关键字字符串。 0表示没有限制,在A中应避免使用连接字符串,因为尝试连接时会无限期等待。