SQL 过程无法正常工作/使用 SQL 命令非常慢

发布于 2024-12-25 02:17:17 字数 326 浏览 1 评论 0原文

我有以下场景;

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 技术交流群。

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

发布评论

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

评论(2

我做我的改变 2025-01-01 02:17:17

执行不返回数据的存储过程

不调用 SqlCommand.ExecuteReader(),而是调用 SqlCommand.ExecuteNonQuery()

根据此 MSDN 参考

执行读取器

执行返回行的命令。为了提高性能,
ExecuteReader 使用 Transact-SQL sp_executesql 调用命令
系统存储过程。因此,ExecuteReader 可能没有
如果用于执行 Transact-SQL 等命令,则可以达到您想要的效果
SET 语句。

执行非查询

执行 Transact-SQL INSERT、DELETE、UPDATE 和 SET 等命令
声明。

编辑

执行存储过程来检索数据

或者,如果您想要返回标量值,则可以使用SqlCommand.ExecuteScalar()。但是,如果您希望获取数据,则需要使用 SqlDataReader 对象,如下所示:

Dim queryString As String = "usp_getdata"

    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        command.CommandType = CommandType.StoredProcedure
        connection.Open()

        Dim reader As SqlDataReader = command.ExecuteReader()

        ' Call Read before accessing data.
        While reader.Read()
            Console.WriteLine(String.Format("{0}, {1}", _
                reader(0), reader(1)))
        End While

        ' Call Close when done reading.
        reader.Close()
    End Using

SqlDataReader 类参考

Execute a stored procedure with no data return

Instead of calling SqlCommand.ExecuteReader(), call SqlCommand.ExecuteNonQuery().

As per this MSDN reference:

ExecuteReader

Executes commands that return rows. For increased performance,
ExecuteReader invokes commands using the Transact-SQL sp_executesql
system stored procedure. Therefore, ExecuteReader might not have the
effect that you want if used to execute commands such as Transact-SQL
SET statements.

ExecuteNonQuery

Executes commands such as Transact-SQL INSERT, DELETE, UPDATE, and SET
statements.

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 a SqlDataReader object, like so:

Dim queryString As String = "usp_getdata"

    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)
        command.CommandType = CommandType.StoredProcedure
        connection.Open()

        Dim reader As SqlDataReader = command.ExecuteReader()

        ' Call Read before accessing data.
        While reader.Read()
            Console.WriteLine(String.Format("{0}, {1}", _
                reader(0), reader(1)))
        End While

        ' Call Close when done reading.
        reader.Close()
    End Using

SqlDataReader Class Reference

千纸鹤 2025-01-01 02:17:17

代码非常慢/无法工作;

  Dim cmd As New SQLCommand
  cmd.Connection = myopenconnection
  cmd.CommandText = "usp_getdata"
  cmd.Parameters.AddWithValue("@Year", 2008)
  cmd.CommandType = StoredProcedure
  Dim reader = cmd.ExecuteReader

通过将上述代码修改为,问题得到解决;

 Dim cmd As New SQLCommand
 cmd.Connection = myopenconnection
 cmd.Parameters.AddWithValue("@Year", 2008)
 cmd.CommandText = "Exec usp_getdata @Year With Recompile"
 cmd.CommandType = Text
 Dim reader = cmd.ExecuteReader

Very slow/not working code;

  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 problem was solved by modifiying the above code to;

 Dim cmd As New SQLCommand
 cmd.Connection = myopenconnection
 cmd.Parameters.AddWithValue("@Year", 2008)
 cmd.CommandText = "Exec usp_getdata @Year With Recompile"
 cmd.CommandType = Text
 Dim reader = cmd.ExecuteReader
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文