.Net SQLCommand在执行没有返回记录的存储过程时超时

发布于 2024-09-15 18:19:17 字数 534 浏览 0 评论 0原文

我想知道是否有人可以提供帮助?

我有一个 SqlCommand 对象的问题,当它执行不返回记录的存储过程时,将会超时。

存储过程并不复杂,它只是一个简单的 SELECT ColumnA, ColumnB, ... FROM TableA WHERE Id = @Id 类型的东西。如果我在 Sql Managment Studio 中运行 SP,它会立即返回。

但是,当我尝试执行命令来填充 DataAdapter 时,或从“立即窗口”手动执行命令时 - 一旦创建了它并填充了参数,它总是会超时。

我使用 SqlCommandBuilder 的 DeriveParameters() 方法来填充 SqCommand 参数,然后迭代集合并填充值。然后,我将 DataAdapter.SelectCommand 设置为 SqlCommand 的引用并调用 DataAdapter 的 fill 方法。

该代码似乎适用于任何返回数据的 SP,但在没有返回行时会出现问题。

有没有人经历过这种情况,可以指出我正确的方向吗?

提前致谢, 问候, 杜安。

I wonder if any one may be able to help?

I have an issue with a SqlCommand object that when it executes a stored procedure that returns no records, will timeout.

The stored procedure is not complicated, it is just a simple SELECT ColumnA, ColumnB, ... FROM TableA WHERE Id = @Id type of thing. If I run the SP in Sql Managment Studio it returns in a flash.

However when I try to excecute the command to fill a DataAdapter, or execute the command manually from the "Immediate Window" - once it has been created and parameters populated, it will always timeout.

I use the SqlCommandBuilder's DeriveParameters() method to populate the SqCommand parameters,and then iterate through the collection and populate the values. I then set the DataAdapter.SelectCommand to a reference of the SqlCommand and call the DataAdapter's fill method.

The code seems to work fine with any SP that returns data, but baulks when no rows are returned.

Has any one experienced this, and can point me in the correct direction, please?

Thanks in advance,
Regards,
Duane.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

允世 2024-09-22 18:19:17

最后发现代码的另一部分在我们正在读取的记录上打开了一个事务。

之所以有多个数据库连接,是因为该项目当前使用ADO和ADO.Net(这是一个非常大的项目,目前正在从VB6转换为.Net。仍然有很多遗留数据访问使用阿杜)。 ADO 连接使事务保持打开状态,并且新的 ADO.Net 连接在事务结束之前无法读取,这种情况只有在 ADO.Net 命令超时、抛出错误且 ADO 事务回滚之后才会发生!

哎哟!

感谢所有阅读并思考该解决方案的人。感谢贝丝的建议。

问候,
杜安。

In the end it turned out that another part of the code had a transaction open on the record we were reading.

The reason for more than one connection to the database is because the project currently uses ADO and ADO.Net (It is a very large project and is currently being from converted from VB6 to .Net. There is still a lot of legacy data access using ADO). The ADO connection had the transaction held open, and the new ADO.Net connection could not read until the transaction ended, which would only happen AFTER the ADO.Net command timed out and the error was thrown and the ADO transaction rolled-back!

Doh!

Thanks for all who read and thought about the solution. Thanks to Beth for her suggestion.

Regards,
Duane.

罪#恶を代价 2024-09-22 18:19:17

查找命令超时属性并将其值设置为零。

    Dim cmd As SqlCommand

    cmd = New SqlCommand
    cmd.CommandText = spName
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Connection = _sqlConn
    cmd.CommandTimeout = 0

look for a command timeout property and set its value to zero.

    Dim cmd As SqlCommand

    cmd = New SqlCommand
    cmd.CommandText = spName
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Connection = _sqlConn
    cmd.CommandTimeout = 0
伤感在游骋 2024-09-22 18:19:17
com = new SqlCommand("insert1",con);

com.CommandType = System.Data.CommandType.StoredProcedure;

com.Parameters.Add("@eno",eno.Text);
com.Parameters.Add("@ename",ename.Text);
com.Parameters.Add("@sal",sal.Text);
Response.Write(com.ExecuteNonQuery().ToString());
com = new SqlCommand("insert1",con);

com.CommandType = System.Data.CommandType.StoredProcedure;

com.Parameters.Add("@eno",eno.Text);
com.Parameters.Add("@ename",ename.Text);
com.Parameters.Add("@sal",sal.Text);
Response.Write(com.ExecuteNonQuery().ToString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文