|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi
I'm trying to get the InfoMessage code below working. What I'm finding is that the print statements have to be before the select statement, which kind of defies the point in my scenario. e.g. here the event will fire for 'hi mum' but not for 'hi again' What am I doing wrong here? thanks <add name="Northwind" connectionString="Integrated Security=SSPI;database=Northwind;server=localhost; Connect Timeout=30; Persist Security Info=False" /> private void ShowPrintStatements() { DbProviderFactory factory = SqlClientFactory.Instance; ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["Northwind"]; string theConnectionString = settings.ConnectionString; DbConnection connection = factory.CreateConnection(); if (connection is SqlConnection) { ((SqlConnection)connection).InfoMessage += new SqlInfoMessageEventHandler(Chapter4_InfoMessage); } connection.ConnectionString = theConnectionString; connection.Open(); DbCommand command = factory.CreateCommand(); command.Connection = connection; command.CommandType = CommandType.Text; command.CommandText = "print 'hi mum';select 1 as somecolumn rint'hi again'"; DbDataReader reader = command.ExecuteReader(); reader.Close(); } void Chapter4_InfoMessage(object sender, SqlInfoMessageEventArgs e) { string message = e.Message; } |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
codefragment@googlemail.com (codefragment@googlemail.com) writes:
> I'm trying to get the InfoMessage code below working. What I'm > finding is that the print statements have to be before the select > statement, which kind of defies the point in my scenario. > e.g. here the event will fire for 'hi mum' but not for 'hi again' > What am I doing wrong here? You need to call .NextResult to get the second message. To do thing properly, you should always iterate over .NextResult until returns NULL, to be sure that you get all results and messages. -- Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se Links for SQL Server Books Online: SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
> You need to call .NextResult to get the second message. To do thing
> properly, you should always iterate over .NextResult until returns NULL, > to be sure that you get all results and messages. Yep, that did it, thanks :-) |
|
![]() |
| Outils de la discussion | |
|
|