SQL Server XML查询工具(附源码)

关键代码:

None.gif private   void  btnSearch_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif  
string s = "<?xml version="1.0" encoding="utf-8"?><SearchResult>";
InBlock.gif  
string endWith = "";
InBlock.gif
InBlock.gif  
// 创建连接对象实例
InBlock.gif
  SqlConnection myConnection = 
InBlock.gif      
new SqlConnection(ConfigurationSettings.AppSettings["ConnectString"]);
InBlock.gif  SqlCommand myCommand 
= new SqlCommand(txtCondition.Text, myConnection);
InBlock.gif  myCommand.CommandType 
= CommandType.Text;
InBlock.gif
InBlock.gif  
// 创建 XML Reader,并读取数据到 XML 字符串中
InBlock.gif
  XmlTextReader xmlReader = null;
InBlock.gif
InBlock.gif  
try
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
// 打开数据库连接
InBlock.gif
    myConnection.Open();
InBlock.gif
InBlock.gif    
// 运行存储过程并初始化 XmlReader
InBlock.gif
    xmlReader = (XmlTextReader)myCommand.ExecuteXmlReader();
InBlock.gif
InBlock.gif    
while(xmlReader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
if (xmlReader.NodeType == XmlNodeType.Element) 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        s 
+= "<" + xmlReader.Name;
InBlock.gif        
InBlock.gif        
if (xmlReader.IsEmptyElement) 
InBlock.gif          endWith 
="/";
InBlock.gif        
else
InBlock.gif          endWith 
= "";
InBlock.gif        
InBlock.gif        
if (xmlReader.HasAttributes) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          
while(xmlReader.MoveToNextAttribute())
InBlock.gif            s 
+= " " + xmlReader.Name + "="" + ToMIMEString(xmlReader.Value) + """;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        s 
+= endWith + ">";
InBlock.gif        
ExpandedSubBlockEnd.gif      }

InBlock.gif      
else if (xmlReader.NodeType == XmlNodeType.EndElement)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        s 
+= "</" + xmlReader.Name + ">";
ExpandedSubBlockEnd.gif      }
 
InBlock.gif      
else if (xmlReader.NodeType == XmlNodeType.Text) 
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
if (xmlReader.Value.Length != 0
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          s 
+= ToMIMEString(xmlReader.Value);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    s
+="</SearchResult>";
InBlock.gif    txtResult.Text 
= s;        
ExpandedSubBlockEnd.gif  }

InBlock.gif  
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    txtResult.Text 
= "Got an error";
InBlock.gif    
//不处理任何错误
ExpandedSubBlockEnd.gif
  }

InBlock.gif  
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
// 关闭数据库连接并清理 reader 对象
InBlock.gif
    myConnection.Close();
InBlock.gif    xmlReader 
= null;
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  XmlDocument xmlDoc 
= new XmlDocument();
InBlock.gif  xmlDoc.LoadXml(s);
InBlock.gif  
//=============================================
InBlock.gif  
//将结果写入
InBlock.gif  
//=============================================
InBlock.gif
  try
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    MemoryStream ms 
= new MemoryStream();
InBlock.gif    XmlTextWriter xtw 
= new XmlTextWriter(ms, Encoding.UTF8);
InBlock.gif    xtw.Formatting 
= Formatting.Indented;
InBlock.gif    xmlDoc.Save(xtw);
InBlock.gif    
byte[] buf = ms.ToArray();
InBlock.gif    txtResult.Text 
= Encoding.UTF8.GetString(buf,0,buf.Length);
InBlock.gif    xtw.Close();
ExpandedSubBlockEnd.gif  }

InBlock.gif  
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    txtResult.Text 
= "出现错误!";
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif  
None.gif
private   string  ToMIMEString( string  s)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  StringBuilder sb 
= new StringBuilder();
InBlock.gif
InBlock.gif  
char[] source = s.ToCharArray();
InBlock.gif  
foreach(char c in source)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
if(c=='<')
InBlock.gif      sb.Append(
"&lt;");
InBlock.gif    
else if(c=='&')
InBlock.gif      sb.Append(
"&amp;");
InBlock.gif    
else if(c=='>')
InBlock.gif      sb.Append(
"&gt;");
InBlock.gif    
else if(c=='"')
InBlock.gif      sb.Append(
"&quot;");
InBlock.gif    
else
InBlock.gif      sb.Append(c);
ExpandedSubBlockEnd.gif  }

InBlock.gif  
return sb.ToString();
ExpandedBlockEnd.gif}


数据库连接可以通过修改 XML_Search.exe.config 文件实现。程序对本地默认SQL实例的Northwind数据库执行XML查询。代码下载请点 这里

转: http://www.cnblogs.com/zhenyulu/articles/42425.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值