如何将XML从C#传递到SQL Server 2008中的存储过程?
我想将xml文件传递给sql server存储过程,如下所示:
CREATE PROCEDURE BookDetails_Insert (@xml xml)
我想比较一些字段数据与其他表数据,如果匹配,则必须将记录插入到表中。
要求:
-
如何将XML传递给存储过程? 我试过这个,但它不起作用: [工作]
command.Parameters.Add( new SqlParameter("@xml", SqlDbType.Xml) { Value = new SqlXml(new XmlTextReader(xmlToSave.InnerXml, XmlNodeType.Document, null)) });
-
如何访问存储过程中的XML数据?
编辑: [工作]
String sql = "BookDetails_Insert"; XmlDocument xmlToSave = new XmlDocument(); xmlToSave.Load("C:\Documents and Settings\Desktop\XML_Report\Books_1.xml"); SqlConnection sqlCon = new SqlConnection("..."); using (DbCommand command = sqlCon.CreateCommand()) { **command.CommandType = CommandType.StoredProcedure;** command.CommandText = sql; command.Parameters.Add( new SqlParameter("@xml", SqlDbType.Xml) { Value = new SqlXml(new XmlTextReader(xmlToSave.InnerXml , XmlNodeType.Document, null)) }); sqlCon.Open(); DbTransaction trans = sqlCon.BeginTransaction(); command.Transaction = trans; try { command.ExecuteNonQuery(); trans.Commit(); sqlCon.Close(); } catch (Exception) { trans.Rollback(); sqlCon.Close(); throw; }
编辑2 :如何创建选择查询以选择页面,描述基于某些条件。
700001048 01048B https://www.landt.com/Books/large/00/7010000048.jpg QUICK AND FLUPKE PRANKS AND JOKES QUICK AND FLUPKE
对于问题的第2部分,请参阅我对存储过程的回答:将XML作为参数传递,并将INSERT(键/值对)作为如何在存储过程中使用XML的示例。
编辑 :下面的示例代码基于评论中给出的具体示例。
declare @MyXML xml set @MyXML = ' 700001048 01048B https://www.landt.com/Books/large/00/70100048.jpg QUICK AND FLUPKE PRANKS AND JOKES QUICK AND FLUPKE - CATASTROPHE QUICK AND FLUPKE ' select Book.detail.value('(isbn_13/text())[1]','varchar(100)') as isbn_13, Book.detail.value('(isbn_10/text())[1]','varchar(100)') as isbn_10, Book.detail.value('(Image_URL/text())[1]','varchar(100)') as Image_URL, Book.detail.value('(title/text())[1]','varchar(100)') as title, Book.detail.value('(Description/text())[1]','varchar(100)') as Description from @MyXML.nodes('/booksdetail') as Book(detail)
如https://support.microsoft.com/kb/555266中所述 ,您需要将xml数据作为NText传递。
您可以按如下方式查询XML变量:
DECLARE @PeopleXml XML SET @PeopleXml = ' James 28 Jane 24 ' -- put [1] at the end to ensure the path expression returns a singleton. SELECT pcvalue('Person[1]/Name[1]', 'varchar(50)') FROM @PeopleXml.nodes('People') p(c) -- table and column aliases
public static string UpdateStaticCertificateFormateNo1Data(StaticCertificateFormatNo1LogicLayer StaticFormat1Detail) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()); con.Open(); string strXMLRegistrationDetails, strXMLQutPut = " "; System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(StaticFormat1Detail.GetType()); System.IO.MemoryStream stream = new System.IO.MemoryStream(); x.Serialize(stream, StaticFormat1Detail); stream.Position = 0; XmlDocument xd = new XmlDocument(); xd.Load(stream); strXMLRegistrationDetails = xd.InnerXml; SqlTransaction trn = con.BeginTransaction(); try { SqlParameter[] paramsToStore = new SqlParameter[2]; paramsToStore[0] = ControllersHelper.GetSqlParameter("@StaticFormat1Detail", strXMLRegistrationDetails, SqlDbType.VarChar); paramsToStore[1] = ControllersHelper.GetSqlParameter("@OutPut", strXMLQutPut, SqlDbType.VarChar); SqlHelper.ExecuteNonQuery(trn, CommandType.StoredProcedure, "UPS_UpdateStaticCertificateFormateNo1Detail", paramsToStore); trn.Commit(); } catch (Exception ex) { trn.Rollback(); con.Close(); if (ex.Message.Contains("UNIQUE KEY constrastring")) { return "Details already in List"; } else { return ex.Message; } } con.Close(); return "Details successfully Added..."; }
您将主要使用xPath和XQuery来查询和修改XML数据。
这是一个很好的起点https://msdn.microsoft.com/en-us/library/ms190798.aspx 。
我真的不能更具体,因为你的问题非常模糊。 如果您需要有关如何使用XPath和XQuery的帮助,请询问有关如何执行操作的具体问题。
上述就是C#学习教程:如何将XML从C#传递到SQL Server 2008中的存储过程?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/959667.html