我想把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,null)) }); sqlCon.Open(); DbTransaction trans = sqlCon.BeginTransaction(); command.Transaction = trans; try { command.ExecuteNonQuery(); trans.Commit(); sqlCon.Close(); } catch (Exception) { trans.Rollback(); sqlCon.Close(); throw; }
<booksdetail> <isn_13>700001048</isbn_13> <isn_10>01048B</isbn_10> <Image_URL>http://www.landt.com/Books/large/00/7010000048.jpg</Image_URL> <title>QUICK AND FLUPKE</title> <Description> PRANKS AND JOKES QUICK AND FLUPKE </Description> </booksdetail>
对于您的问题的第2部分,请参阅我对
Stored procedure: pass XML as an argument and INSERT (key/value pairs)的回答,了解如何在存储过程中使用XML的示例。
原文链接:https://www.f2er.com/xml/293412.html编辑:下面的示例代码基于注释中给出的具体示例。
declare @MyXML xml set @MyXML = '<booksdetail> <isbn_13>700001048</isbn_13> <isbn_10>01048B</isbn_10> <Image_URL>http://www.landt.com/Books/large/00/70100048.jpg</Image_URL> <title>QUICK AND FLUPKE</title> <Description> PRANKS AND JOKES QUICK AND FLUPKE - CATASTROPHE QUICK AND FLUPKE </Description> </booksdetail>' 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)