我有一个带有2个参数的存储过程,返回结果是PRINT
消息。我可以传递参数并运行存储过程,但我无法弄清楚如何返回结果。
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
Dim SQL As New SQLControl
Dim cmd As New SqlCommand
Private Sub RunSql_Click(sender As System.Object, e As System.EventArgs) Handles RunSql.Click
Try
If SQL.HasConnection = True Then
SQL.SQLCon.Open()
cmd.Connection = SQL.SQLCon
cmd.CommandText = "Exec stored procedure " & param1.Text & "," & param2.Text
cmd.ExecuteNonQuery()
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
SQL.SQLCon.Close()
End Try
End Sub
End Class
提前致谢。
答案 0 :(得分:1)
您可以从SQL Server检索警告和信息性消息 数据源使用SqlConnection对象的InfoMessage事件。
在类似的问题中已经回答了这个问题: Get the value of print statement of sql server in vb.net by using oledb
答案 1 :(得分:1)
谢谢!我使用你的链接做了一些挖掘,我已经开始工作了。
如果其他人需要一个例子,则下面修改了代码。
Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Form1
Dim SQL As New SQLControl
Dim cmd As New SqlCommand
Private Sub RunSql_Click(sender As System.Object, e As System.EventArgs) Handles RunSql.Click
Try
If SQL.HasConnection = True Then
SQL.SQLCon.Open()
cmd.Connection = SQL.SQLCon
cmd.CommandText = "Exec stored procedure " & param1.Text & "," & param2.Text
cmd.ExecuteNonQuery()
AddHandler SQL.SQLCon.InfoMessage, New SqlInfoMessageEventHandler(AddressOf OnInfoMessage)
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
SQL.SQLCon.Close()
End Try
End Sub
Private Shared Sub OnInfoMessage(sender As Object, args As SqlInfoMessageEventArgs)
Dim err As SqlError
For Each err In args.Errors
MsgBox(err.Message)
Next
End Sub
End Class
再次感谢, 路易斯