I am using SQL Management objects to connect to a SQL server. They currently contain simple "create table" commands for my example.
I run this code twice to cause an error for "the table already exists."
However, my events below do not fire.
Does anyone have any idea how I can get this message in my code and then modify the ExecutionType to stop the errors causing the exceptions (which I don't want to do, I want to continue)
My code is:
public void executeSomeSQL() { FileInfo file = new FileInfo(@"\c:\sqlcommands.sql"); string script = file.OpenText().ReadToEnd(); SqlConnection conn = new SqlConnection(sqlConnectionString); conn.InfoMessage +=new SqlInfoMessageEventHandler(conn_InfoMessage); Server server = new Server(new ServerConnection(conn)); server.ConnectionContext.InfoMessage += new SqlInfoMessageEventHandler(ConnectionContext_InfoMessage); server.ConnectionContext.ExecuteNonQuery(script,ExecutionTypes.ContinueOnError); MessageBox.Show("All Done"); }
Developments: -
public void conn_InfoMessage(object sender, SqlInfoMessageEventArgs e) { textBox3.Text += "1:"+DateTime.Now.ToString(); } public void ConnectionContext_InfoMessage(object sender, SqlInfoMessageEventArgs e) { textBox3.Text += "2:" + DateTime.Now.ToString(); }
source share