SQL Server stored procedure parsing XML pass-through reference scheme

Source: Internet
Author: User
Tags throw exception

1. Define Stored procedures

-- =============================================
--Defining stored procedures
-- =============================================
CREATE PROCEDURE [dbo]. [Updatet]
-Define XML parameters
@xml XML
As
BEGIN
SET NOCOUNT on;

DECLARE @xmlHandle int

--Output parameters

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 calling stored procedures

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. The XML format is as follows

<Root>
<mem mid= "1" mname= "Xiao Zhao" msex= "male" >
</Mem>>
</Root>

SQL Server stored procedure parsing XML pass-through reference scheme

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.