SqlServer参数化查询之where in和like实现之xml和DataTable传参介绍
来源:asp之家 发布时间:2012-05-22 18:38:49
方案5 使用xml参数
对sql server xml类型参数不熟悉的童鞋需要先了解下XQuery概念,这里简单提下XQuery 是用来从 XML 文档查找和提取元素及属性的语言,简单说就是用于查询xml的语言说到这就会牵着到XPath,其实XPath是XQuery的一个子集,XQuery 1.0 和 XPath 2.0 共享相同的数据模型,并支持相同的函数和运算符,XPath的方法均适用于XQuery,假如您已经学习了 XPath,那么学习 XQuery 也不会有问题。
XQuery概念了解后需要进一步了解下Sql Server对xml的支持函数,主要为query()、nodes()、exist()、value()、modify() ,详见http://msdn.microsoft.com/zh-cn/library/ms190798.aspx
使用xml方式实现where in时有两种实现方式,使用value和exist,在这里推荐使用exist方法,msdn是这样描述的:
D.使用 exist() 方法而不使用 value() 方法
由于性能原因,不在谓词中使用 value() 方法与关系值进行比较,而改用具有 sql:column() 的 exist()。
http://msdn.microsoft.com/zh-cn/library/ms178030.aspx
使用xml的value方法实现(不推荐)
代码如下:
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
string xml = @"
<root>
<UserID>1</UserID>
<UserID>2</UserID>
<UserID>5</UserID>
</root>";
SqlCommand comm = conn.CreateCommand();
//不推荐使用value方法实现,性能相对exist要低
comm.CommandText = @"select * from Users
where exists
(
select 1 from @xml.nodes('/root/UserID') as T(c)
where T.c.value('text()[1]','int')= Users.UserID
)";
//也可以这样写,结果是一样的
//comm.CommandText = @"select * from Users
// where UserID in
// (
// select T.c.value('text()[1]','int') from @xml.nodes('/root/UserID') as T(c)
// )
comm.Parameters.Add(new SqlParameter("@xml", SqlDbType.Xml) { Value = xml });
using (SqlDataAdapter adapter = new SqlDataAdapter(comm))
{
adapter.SelectCommand = comm;
adapter.Fill(dt);
}
}
使用xml的exist方法实现(推荐)
代码如下:
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
string xml = @"
<root>
<UserID>1</UserID>
<UserID>2</UserID>
<UserID>5</UserID>
</root>";
SqlCommand comm = conn.CreateCommand();
//使用xml的exist方法实现这样能够获得较高的性能
comm.CommandText = @"select * from Users where @xml.exist('/root/UserID[text()=sql:column(""UserID"")]')=1";
comm.Parameters.Add(new SqlParameter("@xml", SqlDbType.Xml) { Value = xml });
using (SqlDataAdapter adapter = new SqlDataAdapter(comm))
{
adapter.SelectCommand = comm;
adapter.Fill(dt);
}
}
列举下不同xml结构的查询方法示例,在实际使用中经常因为不同的xml结构经常伤透了脑筋
代码如下:
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
string xml = @"
<root>
<User>
<UserID>1</UserID>
</User>
<User>
<UserID>2</UserID>
</User>
<User>
<UserID>5</UserID>
</User>
</root>";
SqlCommand comm = conn.CreateCommand();
//不推荐使用value方法实现,性能相对exist要低
comm.CommandText = @"select * from Users
where UserID in
(
select T.c.value('UserID[1]','int') from @xml.nodes('/root/User') as T(c)
)";
//也可以这样写,结果是一样的
//comm.CommandText = @"select * from Users
// where exists
// (
// select 1 from @xml.nodes('/root/User') as T(c)
// where T.c.value('UserID[1]','int') = Users.UserID
// )";
comm.Parameters.Add(new SqlParameter("@xml", SqlDbType.Xml) { Value = xml });
using (SqlDataAdapter adapter = new SqlDataAdapter(comm))
{
adapter.SelectCommand = comm;
adapter.Fill(dt);
}
}
代码如下:
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connectionString))
{
string xml = @"
<root>
<User>
<UserID>1</UserID>
</User>
<User>
<UserID>2</UserID>
</User>
<User>
<UserID>5</UserID>
</User>
</root>";
SqlCommand comm = conn.CreateCommand();
//使用xml的exist方法实现这样能够获得较高的性能
comm.CommandText = @"select * from Users where @xml.exist('/root/User[UserID=sql:column(""UserID"")]')=1";
comm.Parameters.Add(new SqlParameter("@xml", SqlDbType.Xml) { Value = xml });
using (SqlDataAdapter adapter = new SqlDataAdapter(comm))
{
adapter.SelectCommand = comm;
adapter.Fill(dt);
}
}
猜你喜欢
- 在MySQL中,使用auto_increment类型的id字段作为表的主键,并用它作为其他表的外键,形成“主从表结构”,这是数据库设计中常见
- 如何选择速度最快的站点? <html><head><meta http-equiv=&qu
- 迭代器模式迭代器模式(Iterator Pattern)是一种常用的设计模式,用于遍历集合中的元素,不暴露集合的内部结构。迭代器模式将集合和
- 【名称】Abs【类别】数学函数【原形】Abs(number)【参数】必选的。Number参数是一个任何有效的数值型表达式【返回值】同numb
- Image and text elements that appear in another element are called floa
- 以下是引用片段:Function c2u(myText) Dim i c2u = "" For i = 1 to Len
- 研究编码,得知GB2312编码与区位码的关系,尝试之后,得此程序。搜索,似乎没人写,故发此地。1.简述(1)GB2312标准的定义,其实就是
- 因为外贸网站,禁止同行抄袭,所以防止中国ip访问访问,访问的时候有密码提示,这样的代码如何写.请给一个提示.或者有好的代码,请分享下。 &n
- 在推广Web标准的今天,那些崇尚Web标准的人经常说XHTML比HTML更加严格,当然从某种意义上说是的,比如它要求所有的标签关闭并且所有的
- 1.在查询分析器理启动或停止SQL Agent服务启动:use mastergoxp_cmdshell 'net start SQL
- 序言本文所提及的VTD-XML并非本文作者原创,作者只是对它进行介绍。问题通常当我们提起XML的使用时,最头痛的部分便是XML的verbos
- SQL的扩展的删除与恢复 删除 代码如下:use master exec spdropextendedproc “xpcmdshell“ e
- asp之日期和时间函数示例可以使用日期和时间函数来得到各种格式的日期和时间函数语法说明示例NowNow()取得系统当前的日期和
- 如何在线更改Windows2000管理者密码?changepwd.htm <html><head><title
- 由于在遭遇到这个页面之前我们一 * 互刚好在讨论交互设计原则之类的话题,其中有一条是:包容性,即满足主体用户需求的同时,尽可能兼顾非主体用户需
- 因为他的简单实用和数量庞大的插件,所以我们喜欢和使用jQuery,在jQuery数量庞大的插件中有很大的一部分是关于图片的,所以今天我们就整
- 有一个古老的争论,是关于在哪里存储应用程序业务逻辑的:是在应用程序本身的业务逻辑层中还是在数据库层中。应用程序逻辑层的绝对支持者提出,数据库
- 测试语法如下:powered by jb51.netexec GetRecordFromPage news,newsid,10,100000
- 生成静态页的方法有很多种,我比较喜欢用xmlhttp的方法生成,因为我不用考虑很多东西,我只要把动态的asp页面编写好就行了。<% s
- 一般一个网站的首页访问量是最大的,如果您的网站的首页打开的非常缓慢,您的客户将会陆续离开你的网站.通常我们把需要经过复杂运算或者查询数据库得