Domanda

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

È stato utile?

Soluzione

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.

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top