码迷,mamicode.com
首页 > 数据库 > 详细

SQL Server 存储过程解析XML传参 参考方案

时间:2015-10-27 17:15:59      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

1、定义存储过程

-- =============================================
--定义存储过程
-- =============================================
CREATE PROCEDURE [dbo].[UpdateT]
-定义xml参数
@xml xml
AS
BEGIN
SET NOCOUNT ON;

DECLARE @xmlHandle int

--输出参数

EXEC sp_xml_preparedocument @xmlHandle OUTPUT, @xml


SELECT MID,MName,MSex into #tmp
FROM OPENXML (@xmlHandle, ‘/Root/Mem‘,1)
WITH (MID  varchar(50),
MName varchar(50),
MSex varchar(10) ) 

Update Mem set MName=t.MName ,MSex=t.MSex from Mem m inner join #tmp t on m.MID=t.MID
EXEC sp_xml_removedocument @xmlHandle

RETURN 

END

2、.NET调用存储过程

public static void ToUpdateHDWRSUMSStatus(string xmlstr)
{
using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.AppSettings["DBConnectionString"].ToString()))
{
connection.Open();
System.Data.SqlClient.SqlTransaction trans = connection.BeginTransaction();
System.Data.SqlClient.SqlCommand testcmd = new System.Data.SqlClient.SqlCommand();
testcmd.Connection = connection;
testcmd.Transaction = trans;
try
{
testcmd.CommandType = CommandType.StoredProcedure;
testcmd.CommandText = "UpdateT";
testcmd.Parameters.Add("@xml", SqlDbType.VarChar, -1).Value = xmlstr;
testcmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception exception)
{
trans.Rollback();
throw exception;
}
finally
{
connection.Close();
}
}
}

3、xml格式如下

<Root>
<Mem MID="1"  MName="小赵" MSex="男">
</Mem>>
</Root>

SQL Server 存储过程解析XML传参 参考方案

标签:

原文地址:http://www.cnblogs.com/zoood/p/4914618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!