gpt4 book ai didi

c# - 多个插入事务连接超时 - ADO.NET - SQL Server

转载 作者:行者123 更新时间:2023-12-02 22:16:21 25 4
gpt4 key购买 nike

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

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 中执行它,它也能正常工作。

连接超时 设置为 30

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

最佳答案

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

关于c# - 多个插入事务连接超时 - ADO.NET - SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14395846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com