gpt4 book ai didi

sql - ms sql 存储过程返回数据没有输出

转载 作者:行者123 更新时间:2023-12-05 01:31:18 26 4
gpt4 key购买 nike

大家好,我正在尝试查找从没有向其发送参数也没有返回任何输出参数的存储过程中获取数据的示例。虽然它确实显示数据。

我如何从下面使用的代码中获取它?

Dim myCommandSQL As New SqlCommand
Dim myReaderSQL As SqlDataReader = Nothing
Dim intX As Integer = 0
Dim connSql As SqlConnection

Try
connSql = New SqlConnection("Server=sqlprod;" & _
"Database=ISS3_PROD;" & _
"User ID=xxx;" & _
"Password=xxx;" & _
"Trusted_Connection=False;")
connSql.Open()

myCommandSQL.CommandType = CommandType.StoredProcedure
myCommandSQL.CommandText = "Select_Prod"

Dim sqlParReturn1 As System.Data.SqlClient.SqlParameter = myCommandSQL.Parameters.Add("@return_value", SqlDbType.VarChar)

sqlParReturn1.Direction = ParameterDirection.Output
myCommandSQL.ExecuteNonQuery()

MsgBox(sqlParReturn1)

connSql.Close()
myCommandSQL.Dispose()

@return_value 我只是放在那里看看会发生什么,但我什么也没得到。

任何帮助都会很棒!

大卫

最佳答案

如果您为命令分配一个参数,那么您的存储过程应该接受一个参数。此外,如果您将方向指定为 Output ,那么您应该将该参数标记为 OUTPUT在您的存储过程中。

如果您只想要不带任何参数的存储过程的结果,请删除包含 sqlParReturn1 的所有行。 .此外,您的命令不是“非查询”——您 查询数据。要获得它,您应该这样做(我还使用一些更好的实践技术重构了您的代码):

Using connSql As SqlConnection = New SqlConnection(...)
connSql.Open()
Using myCommandSQL As SqlCommand = connSql.CreateCommand()
myCommandSQL.CommandType = CommandType.StoredProcedure
myCommandSQL.CommandText = "Select_Prod"
Using reader As SqlDataReader = myCommandSQL.ExecuteReader()
If reader.HasRows Then
While reader.Read()
// loops through the rows returned
End While
End If
End Using
End Using
End Using

关于sql - ms sql 存储过程返回数据没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9731245/

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