U¿ytkownik "W.G. Ryan eMVP said:
You can use the InfoMessage Event which sends back info about errors witha
severity of 13 or less (I may be off by a number or two on the severity
level but it's in the documentation on .InfoMessage). You can send back
Print statements for instance and catch those.
Well, it looks fine, but I have made some tests and it does not work.
My stored procedure:
Create Procedure MyVeryLongStoredProc
As
RaisError('start first step stored proc', 10, 1) --must be below 11
Waitfor Delay '00:00:01'
RaisError('first step was done', 1, 1)
Print 'second step' --the same efect as RaisError
Waitfor Delay '00:00:05'
Print 'second step was done'
Print 'final step'
Waitfor Delay '00:00:05'
Print 'final step of sp was done'
Go
And then in c#:
using System;
using System.Data.SqlClient;
namespace TestInfoMessageEvent
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
TestInfoMessageEv t = new TestInfoMessageEv();
t.Start();
System.Console.ReadLine();
}
}
public class TestInfoMessageEv
{
SqlConnection mySqlC =
new SqlConnection("Persist Security Info=False;" +
"Integrated Security=SSPI;" +
"server=(local);Database=Testy;");
SqlCommand myC;
public TestInfoMessageEv()
{
mySqlC.InfoMessage +=
new SqlInfoMessageEventHandler(mySqlC_InfoMessage);
}
public void Start()
{
mySqlC.Open();
myC = new SqlCommand("MyVeryLongStoredProc", mySqlC);
myC.ExecuteNonQuery();
mySqlC.Close();
}
private void mySqlC_InfoMessage(object sender,
SqlInfoMessageEventArgs e)
{
System.Console.WriteLine(e.Message.ToString());
}
}
}
All messages are displayed in the same time. I suppose that I should use
additional thread, but I some experimented without positive results:
public void Start()
{
System.Threading.ThreadStart ts = new ThreadStart(ExecMyLongProc);
System.Threading.Thread t = new Thread(ts);
t.Start();
}
private void ExecMyLongProc()
{
mySqlC.Open();
myC = new SqlCommand("MyVeryLongStoredProc", mySqlC);
myC.ExecuteNonQuery();
mySqlC.Close();
}
Could you get me an example?
Regards,
Grzegorz