轉自:http://www.maomao365.com/?p=6923
摘要:
在sql腳本的撰寫中,我們經常使用sql腳本print訊息,作為輸出測驗,
通過獲取print訊息,我們可以快速獲取程式中的錯誤資訊,下文講述通過 SqlInfoMessageEventHandler 獲取print資訊,如下所示:
實驗環境:sqlserver 2008 R2
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { webCrm.SAG tmp = new webCrm.SAG(); protected void Page_Load(object sender, EventArgs e) { /*獲取 sql print的訊息資訊*/ string connectionString = "data source=***********;initial catalog=boss;user id=sa;password=erp;"; using (SqlConnection sqlcon = new SqlConnection(connectionString)) { sqlcon.Open(); sqlcon.InfoMessage += new SqlInfoMessageEventHandler(OnReceivingInfoMessage); /* 查詢某個表上的索引碎片的詳細資訊 */ SqlCommand cmd = new SqlCommand("print '輸出訊息1'; set statistics io on ;select * from [ierror] ; print '輸出sql訊息完畢'", sqlcon); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } Console.Read(); } private void OnReceivingInfoMessage(object sender, SqlInfoMessageEventArgs e) { Response.Write("輸出sql訊息:" + e.Message.ToString()); } } }
----------------------輸出-------------------------------------------------------------------------
輸出sql訊息:輸出訊息1
表 'IError',掃描計數 1,邏輯讀取 1 次,物理讀取 0 次,預讀 0 次,lob 邏輯讀取 0 次,lob 物理讀取 0 次,lob 預讀 0 次,
輸出sql訊息完畢
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/108737.html
標籤:C#
