Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 T-SQL print command & ADO.NET (C#)

Author  Topic 

Leslaw Macherzynski
Starting Member

1 Post

Posted - 2003-03-10 : 09:37:03
How to obtain T-sql print statement result (informational message)
using ADO.NET programming tools (C#)?


robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-03-10 : 09:55:14
Well, PRINT is neither a resultset nor an error message, so I don't think you can reliably retrieve its output. Instead, use RAISERROR to generate custom (error) messages, or use either SELECT to output messages as results, OR use ouput variables in a stored procedure. Books Online has more information on output variables.

Go to Top of Page

Onamuji
Aged Yak Warrior

504 Posts

Posted - 2003-03-10 : 10:02:00
actually ... nice little event called InfoMessage


using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Threading;

namespace Sample
{
public class Sample
{
private static ArrayList InfoMessages = new ArrayList();

[STAThread]
public static void Main(string[] args)
{
SqlConnection connection = new SqlConnection("Server=LOCALHOST;Database=master;Integrated Security=SSPI;");
SqlCommand command = new SqlCommand("PRINT 'hello ' + CURRENT_USER;PRINT 'my name is ' + DB_NAME();PRINT 'goodbye ' + CURRENT_USER;", connection);

connection.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage);

connection.Open();
command.ExecuteNonQuery();
connection.Close();

foreach (string message in InfoMessages)
{
Console.Out.WriteLine(message);
}

Thread.Sleep(2000);
}

private static void OnInfoMessage(object sender, SqlInfoMessageEventArgs e)
{
InfoMessages.Add(e.Message);
}
}
}


Go to Top of Page
   

- Advertisement -