ADO.NET and T-SQL print command

A

Adam K.

Hello,
I want write application, that execute stored procedure (MSSQL 2005).
This procedure looks for example look like this:
CREATE PROCEDURE dbo.Test AS
BEGIN
PRINT 'Hello World!!';
END

I know that i can use ie SqlCommand but how can i read messages printed by
T-SQL command PRINT??

Can anyone help me?

Best regards

Adam
 
A

Alberto Poblacion

Adam K. said:
Hello,
I want write application, that execute stored procedure (MSSQL 2005).
This procedure looks for example look like this:
CREATE PROCEDURE dbo.Test AS
BEGIN
PRINT 'Hello World!!';
END

I know that i can use ie SqlCommand but how can i read messages printed by
T-SQL command PRINT??

Those PRINT messages come back to you in the InfoMessage event of the
SqlConnection object:

using (SqlConnection cn = new SqlConnection(connString))
{
cn.InfoMessage += new SqlInfoMessageEventHandler(myRoutine);
SqlCommand cmd = new SqlCommand("dbo.Test", cn);
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
cmd.ExecuteNonQuery();
//At this point "myRoutine" will have been triggered and received the
text.
}
....

private void myRoutine(object sender, SqlInfoMessageEventArgs e)
{
string receivedPrintText = e.Message;
//...
}
 
A

Adam K.

User "Alberto Poblacion said:
Those PRINT messages come back to you in the InfoMessage event of the
SqlConnection object:

using (SqlConnection cn = new SqlConnection(connString))
{
cn.InfoMessage += new SqlInfoMessageEventHandler(myRoutine);
SqlCommand cmd = new SqlCommand("dbo.Test", cn);
cmd.CommandType = CommandType.StoredProcedure;
cn.Open();
cmd.ExecuteNonQuery();
//At this point "myRoutine" will have been triggered and received the
text.
}
...

private void myRoutine(object sender, SqlInfoMessageEventArgs e)
{
string receivedPrintText = e.Message;
//...
}

It's working, thank you :)

Regards

Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top