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
投稿

猜你喜欢

  • 如何优雅地处理Django中的favicon.ico图标详解

    2023-12-03 09:41:36
  • 解决vue项目中页面调用数据 在数据加载完毕之前出现undefined问题

    2024-05-22 10:28:12
  • Go语言学习教程之声明语法(译)

    2024-02-01 03:44:16
  • 键盘扫描码[比较完整]

    2022-09-26 20:20:48
  • SQL 查询语句积累

    2024-01-27 06:32:18
  • 安装MSSql2005时 “以前的某个程序安装已在安装计算机上创建挂起” 的解决办法

    2024-01-22 08:49:39
  • 交互设计实用指南系列(7)–避免迷路

    2010-01-23 09:52:00
  • 多版本python的pip 升级后, pip2 pip3 与python版本失配解决方法

    2023-06-13 18:57:20
  • Django 用户认证组件使用详解

    2021-05-11 12:44:25
  • Lombok插件安装(IDEA)及配置jar包使用详解

    2022-04-16 02:31:37
  • 深入了解Python的异常处理机制

    2023-09-03 09:25:41
  • 基于Python实现的购物商城管理系统

    2021-08-27 15:06:43
  • 浅谈Python peewee 使用经验

    2023-09-14 14:20:55
  • Python实现带图形界面的炸金花游戏(升级版)

    2023-06-27 08:35:20
  • 从pandas一个单元格的字符串中提取字符串方式

    2022-10-14 21:24:35
  • MySQL变量原理及应用实例

    2024-01-12 17:35:30
  • Python设计模式之建造者模式实例详解

    2021-07-13 17:37:03
  • Python json模块与jsonpath模块区别详解

    2023-09-17 18:07:26
  • python模块如何查看

    2021-10-26 20:08:35
  • pandas pd.read_csv()函数中parse_dates()参数的用法说明

    2023-07-22 04:51:26
  • asp之家 网络编程 m.aspxhome.com