简体   繁体   中英

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

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

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 .

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM