繁体   English   中英

多次插入事务连接超时-ADO.NET-SQL Server

[英]Multiple Insert transaction connection timeout - ADO.NET - SQL Server

我有多个插入的事务。 除了一个外,所有插入件都可以正常工作。 我验证了参数,所有拼写,似乎我没有弄清楚。 它给了我错误:

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! 谢谢!

请删除此帖子。 我感到很...愧...经过数小时的挖掘,结果发现我在Management Studio中进行了一些测试,在那里我测试了一些事务而根本没有提交它们。 因此,它正在等待提交,而我一直在进行尝试,或尝试进行插入...! 真可惜! 为此事道歉。

如果这是一个运行时间很长的操作,则可以尝试更改Command Timeout

  1. 检查您的连接字符串。 也许您的connString var有一些错误
  2. 检查您的SqlDataAdapter命令超时。 它们可能是导致异常的原因(请参阅http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx )通常,此类异常是长时间运行的任务或未提交的交易。

连接超时设置为1

SqlConnection.ConnectionTimeout属性

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

因此,您已将timout设置为1秒。 这是否回答你的问题?

备注:您可以使用连接字符串中的ConnectTimeout或Connection Timeout关键字来设置连接等待超时的时间。 值0表示没有限制 ,并且应在ConnectionString中避免该值,因为连接尝试将无限期等待。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM