Get the value of print statement of sql server in vb.net by using oledb

和自甴很熟 提交于 2019-12-23 00:25:48

问题


I am trying to create an query analyzer and executor.

I wonder that how to get the output message of an transact-sql statement like 'PRINT'

declare @msg varchar(100)

set @msg =  select [column] from [table name] where [column] = [condition]

if @msg = 'SOMEVALUE'
 begin
   print 'This is first statement'
 end
else
 begin
  print 'This is second statement'
 end

can you please help me to get the value of print statement of above code in vb.net

Thanks in advance


回答1:


From MSDN:

You can retrieve warnings and informational messages from a SQL Server data source using the InfoMessage event of the SqlConnection object.

The informational messages that is being referred here includes the value being returned by print command.

Update:

AddHandler myConnection.InfoMessage, New SqlInfoMessageEventHandler(AddressOf OnInfoMessage)

Private Sub OnInfoMessage(ByVal sender As Object, ByVal e As System.Data.SqlClient.SqlInfoMessageEventArgs)
        mySB.AppendLine(e.Message)
End Sub

Where myConnection is you SQL connection and mySB is your String Builder.

e.Message has the value of print.




回答2:


I still suggest using SELECT. No output parameters needed

if @msg = 'SOMEVALUE'
 begin
   select 'This is first statement'
 end
else
 begin
     select 'This is second statement'
 end

This you can get in your VB.NET code via simple ExecuteScalar (Or its SqlClient equivalent if you decide not to use OleDB for SQL Server).



来源:https://stackoverflow.com/questions/22995425/get-the-value-of-print-statement-of-sql-server-in-vb-net-by-using-oledb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!