我想将 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:如何创建一个选择查询来选择页面,基于一些条件的描述。
<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>