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

发布于 2024-07-15 13:21:56 字数 328 浏览 4 评论 0原文

有没有一条非常直接的直接路线? (即 SQL Server 可以读取 XML)

或者,最好解析 XML 并通过 ADO.Net 以通常的方式将其作为单独的行或批量更新进行传输?

我意识到可能有一些解决方案涉及大型复杂的存储过程——虽然我并不完全反对这一点,但我倾向于将大部分业务逻辑放在 C# 代码中。 我见过一个使用 SQLXMLBulkLoad 的解决方案,但它似乎需要相当复杂的 SQL 代码。

作为参考,我将一次处理大约 100 行,每行大约 50 个小数据块(字符串和整数)。 这最终将成为日常批处理工作。

您可以提供的任何代码片段将非常感激。

Is there a direct route that is pretty straight forward? (i.e. can SQL Server read 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?

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. I have seen a solution using SQLXMLBulkLoad, but it seemed to require fairly complex SQL code.

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). This will eventually become a daily batch job.

Any code snippets you can provide would be very much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白首有我共你 2024-07-22 13:21:56

SQL Server 2005 及更高版本有一个名为“XML”的数据类型,您可以在其中存储 XML - 非类型化或使用 XSD 架构类型化。

您基本上可以从 XML 文字字符串填充 XML 类型的列,因此您可以轻松地使用普通的 INSERT 语句并将 XML 内容填充到该字段中。

马克

SQL Server 2005 and up have a datatype called "XML" which you can store XML in - untyped or typed with a XSD schema.

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.

Marc

薄荷→糖丶微凉 2024-07-22 13:21:56

您可以使用函数 OPENXML 和存储过程 sp_xml_preparedocument 轻松地将 XML 转换为行集。

You can use the function OPENXML and stored procedure sp_xml_preparedocument to easily convert your XML into rowsets.

GRAY°灰色天空 2024-07-22 13:21:56

如果您使用的是 SQL Server 2008(或 2005),它具有 xml 本机数据类型。 您可以将 XSD 架构与 xml 变量关联,并直接插入到 xml 类型的列中。

If you are using SQL Server 2008 (or 2005), it has an xml native datatype. You can associate an XSD schema with xml variables, and Insert directly into columns of type xml.

も星光 2024-07-22 13:21:56

是的,SQL Server 2005 及更高版本可以立即解析 XML。

您可以使用节点、值和查询方法来按照您想要的方式分解它,无论是值还是属性

一些无耻的插入:

Yes, SQL Server 2005 and above can parse XML out of the box.

You use the nodes, value and query methods to break it down how you want, whether values or attributes

Some shameless plugging:

℉服软 2024-07-22 13:21:56

Xml 数据和 Xml 文档可能具有不同的含义。
当 xml 类型适合数据时,它不会保存格式(删除空格),因此在某些情况下(例如配置文件),最佳选择是 nvarchar。

Xml data and Xml document could have different meaning.
When xml type is good for data, it doesn't save formatting (white spaces removed), so in some cases (e.g. cofiguration files) the best option is nvarchar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文