sql - what is the Way to get the messages that were displayed in SQLManagementStudio after executing a query in C#(programming) -


this question has answer here:

i trying derive text in "messages" part string variable in c#.net after executing query program.

like,

int messag = convert.toint32(command.executenonquery()) 

which returns no. of rows affected, same way retrieve text "messages" part string variable when executed this.

is possible?

sql query execution message

you can attach event handlers when error or info reported, explained in post.

namespace test {     using system;     using system.data;     using system.data.sqlclient;      public class program     {         public static int main(string[] args)         {             if (args.length != 2)             {                 usage();                 return 1;             }              var conn = args[0];             var sqltext = args[1];             showsqlerrorsandinfo(conn, sqltext);              return 0;         }          private static void usage()         {             console.writeline("usage: sqlserverconnectionstring sqlcommand");             console.writeline("");             console.writeline("   example:  \"data source=.;integrated security=true\" \"dbcc checkdb\"");         }          public static void showsqlerrorsandinfo(string connectionstring, string query)         {             using (sqlconnection connection = new sqlconnection(connectionstring))             {                 connection.statechange += onstatechange;                 connection.infomessage += oninfomessage;                  sqlcommand command = new sqlcommand(query, connection);                 try                 {                     command.connection.open();                     console.writeline("command execution starting.");                     sqldatareader dr = command.executereader();                     if (dr.hasrows)                     {                         console.writeline("rows returned.");                         while (dr.read())                         {                             (int idx = 0; idx < dr.fieldcount; idx++)                             {                                 console.write("{0} ", dr[idx].tostring());                             }                              console.writeline();                         }                     }                      console.writeline("command execution complete.");                 }                 catch (sqlexception ex)                 {                     displaysqlerrors(ex);                 }                                 {                     command.connection.close();                 }             }         }          private static void displaysqlerrors(sqlexception exception)         {             foreach (sqlerror err in exception.errors)             {                 console.writeline("error: {0}", err.message);             }         }          private static void oninfomessage(object sender, sqlinfomessageeventargs e)         {             foreach (sqlerror info in e.errors)             {                 console.writeline("info: {0}", info.message);             }         }          private static void onstatechange(object sender, statechangeeventargs e)         {             console.writeline("connection state changed: {0} => {1}", e.originalstate, e.currentstate);         }     } } 

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -