SQL 过程无法正常工作/使用 SQL 命令非常慢
我有以下场景;
Dim cmd as New SQLCommand
cmd.Connection = myopenconnection
cmd.CommandText = "usp_getdata"
cmd.Parameters.AddWithValue("@Year", 2008)
cmd.CommandType = StoredProcedure
Dim reader = cmd.ExecuteReader
执行上述操作时,应用程序会卡住并一直等待响应。我尝试从 SQL Management studio 执行 SQL 命令,它在数据库的另一个副本上运行良好。
I have the following scenario;
Dim cmd as New SQLCommand
cmd.Connection = myopenconnection
cmd.CommandText = "usp_getdata"
cmd.Parameters.AddWithValue("@Year", 2008)
cmd.CommandType = StoredProcedure
Dim reader = cmd.ExecuteReader
The application get stuck and keep waiting for a response when the above is excuted. I have tryed to execute the SQL command from SQL Management studio and it works fine and on another copy of the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行不返回数据的存储过程
不调用
SqlCommand.ExecuteReader()
,而是调用SqlCommand.ExecuteNonQuery()
。根据此 MSDN 参考:
编辑:
执行存储过程来检索数据
或者,如果您想要返回标量值,则可以使用
SqlCommand.ExecuteScalar()
。但是,如果您希望获取数据,则需要使用SqlDataReader
对象,如下所示:SqlDataReader 类参考
Execute a stored procedure with no data return
Instead of calling
SqlCommand.ExecuteReader()
, callSqlCommand.ExecuteNonQuery()
.As per this MSDN reference:
EDIT:
Execute a stored procedure to retrieve data
Or if what you're trying to do is return a scalar value, you can use
SqlCommand.ExecuteScalar()
. But if you're looking to get data, you need to utilize aSqlDataReader
object, like so:SqlDataReader Class Reference
代码非常慢/无法工作;
通过将上述代码修改为,问题得到解决;
Very slow/not working code;
The problem was solved by modifiying the above code to;