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);
}
}

标签:参数化查询,xml,DataTable
0
投稿

猜你喜欢

  • sqlserver清空service broker中的队列的语句分享

    2011-09-30 11:33:35
  • 设计提升满意度

    2010-05-16 15:00:00
  • 网页版权的正确写法

    2007-09-22 09:13:00
  • Asp截获后台登录密码的代码

    2012-12-04 20:20:38
  • 用CSS实现柱状图(Bar Graph)的方法(二)—基于表格元素的柱状图

    2008-05-26 13:23:00
  • SQL Server 2000 清理日志精品图文教程

    2012-07-21 14:31:17
  • 10分钟学会Google Map API (一)

    2009-06-07 18:17:00
  • [翻译]JavaScript中对象的层次与继承

    2008-12-31 13:36:00
  • 一个css垂直水平居中布局,css效果

    2008-11-03 11:40:00
  • ASP连接Oracle数据库的例子

    2007-10-02 12:44:00
  • 从开发人员角度看IE8的开发新特性

    2010-02-26 10:48:00
  • 超半数中文网页一年内将“消失”

    2008-03-08 12:49:00
  • CSS控制Table表格文字样式

    2008-06-11 18:53:00
  • 如何使用ASP来读写注册表

    2007-09-20 13:08:00
  • asp事务处理的另外一个方法

    2010-05-27 12:18:00
  • 常见数据库系统比较 Oracle数据库

    2010-07-28 12:44:00
  • 让SQL Server数据库自动执行管理任务(一)

    2009-03-20 10:35:00
  • 就 CSS3 向 Eric Meyer 请教的 6 个问题

    2008-12-29 13:52:00
  • MYSQL教程:检查数据表和修复数据表

    2009-03-11 15:24:00
  • 用ASP打造一个小型的网页BBS系统

    2008-10-10 16:36:00
  • asp之家 网络编程 m.aspxhome.com