简体   繁体   English

读取XML数据和构建查询以将值插入SQL Server数据库的最佳方法是什么?

[英]What's the best approach for reading in XML data and building a query to insert the values into a SQL Server DB?

What's the best approach for reading in XML data and building a query to insert the values into a SQL Server DB? 读取XML数据和构建查询以将值插入SQL Server数据库的最佳方法是什么?

Anyone have a best approach for this type of scenario? 对于这种情况,任何人都有最好的方法吗?

Personally, I use a DataSet.ReadXML(). 就个人而言,我使用DataSet.ReadXML()。 Then it places all the data into DataTables in the DataSet and then is much easier to loop through and manipulate the data. 然后,它将所有数据放入DataSet中的DataTables,然后更容易循环和操作数据。

SQL 2005 has very good xml capabilities. SQL 2005具有非常好的xml功能。 This is one technique using XML Parameters 这是一种使用XML参数的技术

Are you aware of the XML features of MS SQL Server 2005/8 ? 您是否了解MS SQL Server 2005/8的XML功能? You can insert XML strong typed directly as a column into SQL, and then enforce that the XML is valid against the specified schema. 您可以将XML强类型直接作为列插入到SQL中,然后强制XML对指定的模式有效。

You can even index columns within the XML and run Xquery against it. 您甚至可以在XML中索引列并对其运行Xquery。

Could we have some more information about what you want to do? 我们可以获得一些关于你想做什么的更多信息吗?

If you are after fairly simple data (ie the first-level children), then an option I've used successfully in the past is to use an XmlReader to read the xml (on child at a time), presenting the data in a lightweight IDataReader that SqlBulkCopy can consume to pump the data in. Like so . 如果您使用相当简单的数据(即第一级子级),那么我过去成功使用的一个选项是使用XmlReader读取xml(一次在子级上),以轻量级呈现数据SqlBulkCopy可以使用的IDataReader来输入数据。 就像这样 If the data is more complex, use sqlxml / bulk load. 如果数据更复杂,请使用sqlxml / bulk load。

编写一个与xml具有类似结构的数据库模式,然后编写一个Linq To Xml和Linq To Sql语句来推送数据。

Pass the data to a stored procedure as an XML document to an variable with the XML data type. 将数据作为XML文档传递给存储过程,并将其传递给XML数据类型的变量。 Then parse the document within the procedure and load the data into a table. 然后在过程中解析文档并将数据加载到表中。

I give examples here . 在这里举例。

Your question is not clear on the details: 您的问题在细节上不明确:

If you have arbitrary XML files you want to insert into a db table, declare an XML column (SQL2005+) and simply insert the data as string. 如果要将任意XML文件插入到db表中,请声明XML列(SQL2005 +)并简单地将数据作为字符串插入。

If you have XML files adhering to an XML schema, you can declare an XML SCHEMA COLLECTION using the XSD, bind the XML column to that xml schema collection, and insert the data. 如果您有XML文件遵循XML模式,则可以使用XSD声明XML SCHEMA COLLECTION,将XML列绑定到该xml架构集合,然后插入数据。 SQL Server will validate every inserted XML value against that XSD. SQL Server将针对该XSD验证每个插入的XML值。

If you want to manually parse the XML data, use XPath or XPathNavigator or XMLDocument.SelectNodes to extract the data, and insert them record by becord into your db tables. 如果要手动解析XML数据,请使用XPath或XPathNavigator或XMLDocument.SelectNodes提取数据,并将它们记录到db表中。

You probably want to be more specific in your question which of the alternatives apply to your case. 您可能希望在您的问题中更具体地说明哪种替代方案适用于您的案例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM