日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SQL Server XML查詢工具

導(dǎo)讀:SQL Server提供了一個(gè)非常好用的客戶端檢索工具-查詢分析器,但是美中不足的是查詢分析器無法對XML查詢給出很好的結(jié)果,用戶無法完整查看XML結(jié)果集。上學(xué)期給IBM電子商務(wù)班講XML與WebService時(shí),不得不自己寫了一個(gè)程序執(zhí)行XML檢索。雖然程序?qū)嵲谟行┖喡?,但畢竟可以完成課堂演示的要求。

程序主體是通過sqlCommand的ExecuteXmlReader方法完成的。我添加了一些對特殊字符的MIME編碼(這一部分應(yīng)當(dāng)有現(xiàn)成代碼,不過我沒有找到,只好自己將就著寫了),以及XML格式處理功能。先貼上一些代碼,具體可以自己下載后看。希望多提寶貴意見。

關(guān)鍵代碼:

 
 
 
  1.  
  2. private void btnSearch_Click(object sender, System.EventArgs e)  
  3. {  
  4.  
  5.   string s = "";  
  6.   string endWith = "";  
  7.  
  8.   // 創(chuàng)建連接對象實(shí)例  
  9.   SqlConnection myConnection =   
  10.       new SqlConnection(ConfigurationSettings.AppSettings["ConnectString"]);  
  11.   SqlCommand myCommand = new SqlCommand(txtCondition.Text, myConnection);  
  12.   myCommand.CommandType = CommandType.Text;  
  13.  
  14.   // 創(chuàng)建 XML Reader,并讀取數(shù)據(jù)到 XML 字符串中  
  15.   XmlTextReader xmlReader = null;  
  16.  
  17.   try  
  18.   {  
  19.     // 打開數(shù)據(jù)庫連接  
  20.     myConnection.Open();  
  21.  
  22.     // 運(yùn)行存儲過程并初始化 XmlReader  
  23.     xmlReader = (XmlTextReader)myCommand.ExecuteXmlReader();  
  24.  
  25.     while(xmlReader.Read())  
  26.     {  
  27.       if (xmlReader.NodeType == XmlNodeType.Element)   
  28.       {  
  29.         s += "<" + xmlReader.Name;  
  30.           
  31.         if (xmlReader.IsEmptyElement)   
  32.           endWith ="/";  
  33.         else  
  34.           endWith = "";  
  35.           
  36.         if (xmlReader.HasAttributes)   
  37.         {  
  38.           while(xmlReader.MoveToNextAttribute())  
  39.             s += " " + xmlReader.Name + "="" + ToMIMEString(xmlReader.Value) + """;  
  40.         }  
  41.  
  42.         s += endWith + ">";  
  43.           
  44.       }  
  45.       else if (xmlReader.NodeType == XmlNodeType.EndElement)  
  46.       {  
  47.         s += "";  
  48.       }   
  49.       else if (xmlReader.NodeType == XmlNodeType.Text)   
  50.       {  
  51.         if (xmlReader.Value.Length != 0)   
  52.         {  
  53.           s += ToMIMEString(xmlReader.Value);  
  54.         }  
  55.       }  
  56.     }  
  57.  
  58.     s+="";  
  59.     txtResult.Text = s;          
  60.   }  
  61.   catch (Exception)  
  62.   {  
  63.     txtResult.Text = "Got an error";  
  64.     //不處理任何錯(cuò)誤  
  65.   }  
  66.   finally  
  67.   {  
  68.     // 關(guān)閉數(shù)據(jù)庫連接并清理 reader 對象  
  69.     myConnection.Close();  
  70.     xmlReader = null;  
  71.   }  
  72.  
  73.   XmlDocument xmlDoc = new XmlDocument();  
  74.   xmlDoc.LoadXml(s);  
  75.   //=============================================  
  76.   //將結(jié)果寫入  
  77.   //=============================================  
  78.   try  
  79.   {  
  80.     MemoryStream ms = new MemoryStream();  
  81.     XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8);  
  82.     xtw.Formatting = Formatting.Indented;  
  83.     xmlDoc.Save(xtw);  
  84.     byte[] buf = ms.ToArray();  
  85.     txtResult.Text = Encoding.UTF8.GetString(buf,0,buf.Length);  
  86.     xtw.Close();  
  87.   }  
  88.   catch  
  89.   {  
  90.     txtResult.Text = "出現(xiàn)錯(cuò)誤!";  
  91.   }  
  92.  
  93. }  
  94.     
  95. private string ToMIMEString(string s)  
  96. {  
  97.   StringBuilder sb = new StringBuilder();  
  98.  
  99.   char[] ssource = s.ToCharArray();  
  100.   foreach(char c in source)  
  101.   {  
  102.     if(c=='<')  
  103.       sb.Append("<");  
  104.     else if(c=='&')  
  105.       sb.Append("&");  
  106.     else if(c=='>')  
  107.       sb.Append(">");  
  108.     else if(c=='"')  
  109.       sb.Append(""");  
  110.     else  
  111.       sb.Append(c);  
  112.   }  
  113.   return sb.ToString();  

數(shù)據(jù)庫連接可以通過修改 XML_Search.exe.config 文件實(shí)現(xiàn)。程序?qū)Ρ镜啬J(rèn)SQL實(shí)例的Northwind數(shù)據(jù)庫執(zhí)行XML查詢。

【編輯推薦】

  1. SQL Server XML 入門的18句話
  2. SQL Server管理工具的升級
  3. 深入討論SQL Server 表的主鍵問題
  4. SQL Server數(shù)據(jù)庫中對圖片進(jìn)行保存和輸出

網(wǎng)頁名稱:SQL Server XML查詢工具
路徑分享:http://www.5511xx.com/article/cdepgsi.html