简体   繁体   English

将 XML 数据保存到 SQL 服务器的最佳方法是什么?

[英]What is the best way to save XML data to SQL Server?

Is there a direct route that is pretty straight forward?有没有一条非常直接的直接路线? (ie can SQL Server read XML) (即SQL服务器可以读取XML)

Or, is it best to parse the XML and just transfer it in the usual way via ADO.Net either as individual rows or perhaps a batch update?或者,最好解析 XML 并通过 ADO.Net 以通常的方式将其作为单独的行或批量更新进行传输?

I realize there may be solutions that involve large complex stored procs--while I'm not entirely opposed to this, I tend to prefer to have most of my business logic in the C# code.我意识到可能存在涉及大型复杂存储过程的解决方案——虽然我并不完全反对这一点,但我倾向于将我的大部分业务逻辑放在 C# 代码中。 I have seen a solution using SQLXMLBulkLoad, but it seemed to require fairly complex SQL code.我见过使用 SQLXMLBulkLoad 的解决方案,但它似乎需要相当复杂的 SQL 代码。

For reference, I'll be working with about 100 rows at a time with about 50 small pieces of data for each (strings and ints).作为参考,我将一次处理大约 100 行,每行大约有 50 个小数据(字符串和整数)。 This will eventually become a daily batch job.这最终将成为每天的批处理作业。

Any code snippets you can provide would be very much appreciated.您可以提供的任何代码片段将不胜感激。

SQL Server 2005 and up have a datatype called "XML" which you can store XML in - untyped or typed with a XSD schema. SQL Server 2005 及更高版本有一个名为“XML”的数据类型,您可以将 XML 存储在其中 - 未键入或使用 XSD 模式键入。

You can basically fill columns of type XML from an XML literal string, so you can easily just use a normal INSERT statement and fill the XML contents into that field.您基本上可以从 XML 文字字符串中填充 XML 类型的列,因此您可以轻松地使用普通的 INSERT 语句并将 XML 内容填充到该字段中。

Marc马克

You can use the function OPENXML and stored procedure sp_xml_preparedocument to easily convert your XML into rowsets.您可以使用 function OPENXML 和存储过程 sp_xml_preparedocument 轻松地将 XML 转换为行集。

If you are using SQL Server 2008 (or 2005), it has an xml native datatype.如果您使用 SQL Server 2008(或 2005),则它具有xml本机数据类型。 You can associate an XSD schema with xml variables, and Insert directly into columns of type xml .您可以将 XSD 模式与xml变量相关联,并直接插入到xml类型的列中。

Yes, SQL Server 2005 and above can parse XML out of the box.是的,SQL Server 2005 及更高版本可以开箱即用地解析 XML。

You use the nodes, value and query methods to break it down how you want, whether values or attributes您使用节点、值和查询方法来分解它,无论是值还是属性

Some shameless plugging:一些无耻的插播:

Xml data and Xml document could have different meaning. Xml 数据和 Xml 文档可能具有不同的含义。 When xml type is good for data, it doesn't save formatting (white spaces removed), so in some cases (eg cofiguration files) the best option is nvarchar.当 xml 类型适用于数据时,它不会保存格式(删除空格),因此在某些情况下(例如配置文件)最好的选择是 nvarchar。

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

相关问题 在SQL Server中保存大型请求和响应xml的最佳实践是什么 - What is best practice to save large request and response xml in SQL Server 在SQL中保存大型每月数据备份的最佳方法是什么? - What's the best way to save large monthly data backups in SQL? 将XML分层数据导入Sql Server表的最佳方法 - Best way to import XML hierarchical data into Sql Server table 将 XML 数据分解为 SQL 服务器数据库列的最佳方法 - The Best Way to shred XML data into SQL Server database columns 在 SQL Server 2005/2008 中存储历史数据的最佳方式是什么? - What is the best way to store historical data in SQL Server 2005/2008? 在SQL Server中对大量数据执行操作的最佳方法是什么? - What is the best way to perform a manipulation with huge amounts of data in SQL Server? 将数据从 SQL Server 迁移到 Oracle 数据库的最佳方法是什么 - what is the best way to migrate data from SQL Server to Oracle database SQL Server 2008:插入大数据的最佳方法是什么? - SQL Server 2008 : What is the best way for inserting big chunk of data? SQL Server-更改PK数据类型的最佳方法是什么? - SQL Server - What's the best way to change a PK data type? 从SQL服务器将数据同步到Azure的最佳方法是什么 - What is the best way to sync data into Azure from a SQL server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM