C#SQL输出到SSMS消息选项卡等文本
我在找到一种方法来获取更新/插入/删除/表创建的结果时遇到了一些麻烦……
我想在SSMS(SQL Server Management Studio)消息选项卡中看到它(猜测该工具是done_in_proc)。
目前,我可以获得exception,并且我可以捕获在SQL脚本中完成的打印。
我已经尝试过ExecuteScalar,ExecuteReader,ExecuteWithResults并且什么也没得到。
谢谢,
码:
上述就是C#学习教程:C#SQL输出到SSMS消息选项卡等文本分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
public void conn_InfoMessage(object sender, SqlInfoMessageEventArgs e) { foreach (SqlError err in e.Errors) { richTextBoxDeployCopy.AppendText("Info : "+err.Message + Environment.NewLine); } } public void ExecSQLScript(string Instance, string Database, string SQLScript, string Username, string Password) { if (Application2Deploy == "DUMMY") { server = "Server"; } else { server = Instance; } string sqlConnectionString = @"User ID=" + Username + "; Password = " + Password + ";Persist Security Info=False;Initial Catalog=" + Database + ";Data Source=" + server + "\" + Instance; FileInfo file = new FileInfo(SQLScript); string script = file.OpenText().ReadToEnd(); conn = new SqlConnection(sqlConnectionString); conn.FireInfoMessageEventOnUserErrors = true; Server SQLserver = new Server(new ServerConnection(conn)); if (checkBoxDeployCopyTestingScript.Checked) { richTextBoxDeployCopy.AppendText("Test: Running SQL Script......" + Environment.NewLine); LogFile(DateTime.Now + "Test: Running SQL Script......", LogPath); } else { try { SQLserver.ConnectionContext.Connect(); SQLserver.ConnectionContext.InfoMessage += new SqlInfoMessageEventHandler(conn_InfoMessage); SQLserver.ConnectionContext.BeginTransaction(); SQLserver.ConnectionContext.ExecuteNonQuery(script); Error = false; } catch (Exception SQLEx) { Error = true; richTextBoxDeployCopy.AppendText("Error executing SQL Script." + Environment.NewLine); LogFile(DateTime.Now + "Error executing SQL Script", LogPath); richTextBoxDeployCopy.AppendText("Exception: " + SQLEx.InnerException.Message + Environment.NewLine); LogFile(DateTime.Now + "Exception: " + SQLEx.InnerException.Message, LogPath); try { SQLserver.ConnectionContext.RollBackTransaction(); } catch (Exception ex2) { richTextBoxDeployCopy.AppendText("Error executing rollback." + Environment.NewLine); richTextBoxDeployCopy.AppendText("Exception: " + ex2.Message + Environment.NewLine); } } if (Error == false) { SQLserver.ConnectionContext.CommitTransaction(); CopyExit("End"); file.OpenText().Close(); file.OpenText().Dispose(); SQLserver.ConnectionContext.SqlConnectionObject.Close(); //SQLserver.ConnectionContext.ForceDisconnected(); } else { CopyExit("Abort"); file.OpenText().Close(); file.OpenText().Dispose(); SQLserver.ConnectionContext.SqlConnectionObject.Close(); //SQLserver.ConnectionContext.ForceDisconnected(); } } }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1002553.html