WEB开发网
开发学院数据库MSSQL Server 使用 SQL Server 2005 中的 SQLCLR 处理 XML Show... 阅读

使用 SQL Server 2005 中的 SQLCLR 处理 XML Showplan

 2007-05-13 09:24:45 来源:WEB开发网   
核心提示: using System;using System.IO;using System.Data;using System.Data.Sql;using System.Data.SqlClient;public class xmlshowplanaccess{public static voi
using System;
using System.IO;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
public class xmlshowplanaccess 
{
 public static void GetXMLShowplan(string tsqlStmt, ref string tsqlStmtShowplan) 
 {
   // tsqlStmt contains the statement whose showplan needs to be returned
   // tsqlStmtShowplan will return the showplan of tsqlStmt in XMLformat
   // Open a connection and create a command
   SqlConnection conn = new SqlConnection("context connection = true");
   conn.Open();
   SqlCommand cmd = conn.CreateCommand();
   cmd.CommandText = "set showplan_xml on";
   cmd.ExecuteNonQuery(); // turn the showplan_xml mode on
   cmd.CommandText = tsqlStmt;
   try 
   {
     // thePlan will contain the showplan in XMLformat
     String thePlan = String.Empty;
     SqlDataReader sdr = cmd.ExecuteReader();
     // In case the result set is chunked, concatenate
     while (sdr.Read()) 
       thePlan += sdr.GetSqlString(0).ToString();
     sdr.Close();
     cmd.CommandText = "set showplan_xml off" ;
     cmd.ExecuteNonQuery(); // turn the showplan_xml mode off
     tsqlStmtShowplan = thePlan; // return the showplan in XMLformat
   } 
   catch (SqlException e) // return well formed xml document with the text of exception
   {
     tsqlStmtShowplan = "" + e.ToString() + "";
   }
 } // GetXMLShowplan ends
} // xmlshowplanaccess ends

上一页  4 5 6 7 8 9 

Tags:使用 SQL Server

编辑录入:coldstar [复制链接] [打 印]
赞助商链接