1:如果参数是int类型:
declare @a xml
set @a='
<row><id>1</id></row>
<row><id>5</id></row>
<row><id>4</id></row>
<row><id>3</id></row>
<row><id>2</id></row>'
select * from product where id in (
select d.x.value('./id[1]','int') from @a.nodes('/*') as d(x))
2:如果参数是varchar类型:
declare @a xml
set @a='
<row><name>a</name></row>
<row><name>b5</name></row>
<row><name>c4</name></row>
<row><name>d3</name></row>
<row><name>e2</name></row>'
select * from product where pname in (
select d.x.value('./name[1]','varchar(100)') from @a.nodes('/*') as d(x))
public DataSet GetData(List<int> idList) { System.Text.StringBuilder idXML = new System.Text.StringBuilder(); // 把IdList转换成idxml(后面要用到的xml参数的值) foreach (var item in idList) { idXML.AppendFormat("<row><id>{0}</id></row>",item); } System.Text.StringBuilder strsql = new System.Text.StringBuilder(); strsql.Append("select * from product where id in ("); // 解析xml参数@a 取出ID 这里可以认为@a是一个特殊的表 strsql.Append("select d.x.value('./id[1]','int') from @a.nodes('/*') as d(x)"); strsql.Append(")"); sqlConnection con = new sqlConnection("数据库连接字符串"); sqlCommand cmd = new sqlCommand(strsql.ToString(),con); // 参数赋值 sqlParameter[] para = new sqlParameter[]{ new sqlParameter("@a",sqlDbType.Xml){Value= idXML.ToString()} }; cmd.Parameters = para; // 查询 sqlDataAdapter sda = new sqlDataAdapter(cmd); DataSet ds = new DataSet(); sda.Fill(ds); return ds; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。