在 SQL Server 2000 中存储 XML 数据集
我需要将数据集(报告应用程序的结果)保存到 SQL Server 2000 中的表中。我是否像在数据库中存储文件一样通过 Byte Array() 保存数据集?
I need to save the DataSet, results of a reporting app, to a table in SQL Server 2000. Do I save the datasets the same way I would store files in the DB, via Byte Array()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SQL Server 2000 确实没有很好地处理 XML blob。它既没有专用的 XML 类型,也不容易支持 VARCHAR(MAX) 数据类型形式的大文本。两者都可在 SQL Server 2005 及更高版本中使用。
如果您完全卡在 2000 上,那么最好的选择是将 XML 存储到
TEXT
列中(或NTEXT
- 每个字符 2 个字节 - 如果您需要支持非 ASCII 类型的字符集,如亚洲语言、希伯来语、阿拉伯语等)。但请注意:TEXT 列数据类型并不是很有用,因为您无法在没有很多麻烦的情况下真正对其进行很多操作 - 您甚至无法真正搜索或替换其中的某些内容。真是一团糟。通常的字符串方法都不起作用 - 它实际上是一种“存储后忘记”类型的 blob...
更新:如果您永远需要检查文件的内容,而它存储在数据库中(真的吗?),您还可以使用
IMAGE
数据类型。要将文件存储在 SQL Server 中,请执行以下操作:
对于从数据库中读取字节块,执行基本相同的操作。或者您可以将其打包在一个很好的存储过程中 - 完全取决于您。
如果您使用
TEXT/NTEXT
,您需要存储一个实际的字符串,例如您不能真正使用二进制文件(如Word) - 但如果您从报告中输出XML或HTML或其他内容,那么 TEXT/NTEXT 也可以工作(在这种情况下只需使用 cmd.Parameters.Add("@Content", SqlDbType.Text); )。SQL Server 2000 is really not well equipped to handle XML blobs. It neither has the dedicated XML type, nor does it easily support large text in the form of a VARCHAR(MAX) data type. Both are available in SQL Server 2005 and up.
If you're absolutely and definitely stuck on 2000, your best option is to store your XML into a
TEXT
column (orNTEXT
- 2 byte per character - if you need to support non-ASCII type character sets like Asian languages, Hebrew, Arabic etc).Mind you though : the TEXT column data type is not very useful in that you can't really do much on it without a lot of hassle - you can't even really search or replace something on it. It's a mess. None of the usual string method work on it - it's really a "store-and-forget" kind of blob.....
UPDATE: if you will never need to inspect the contents of the file while it's stored in the database (really?), you could also use an
IMAGE
data type.To store the file in SQL Server, do something like this:
And basically the same thing for the reading out of the blob of bytes from the database. Or you can package this up in a nice stored procedure - totally up to you.
If you use
TEXT/NTEXT
, you need to store an actual string, e.g. you could not really use a binary file (like Word) - but if you have output from a report as XML or HTML or something, then TEXT/NTEXT would work, too (just usecmd.Parameters.Add("@Content", SqlDbType.Text);
in that case).您还可以
序列化
(二进制而不是xml)您的数据集并保存在数据库中。you can also
serialize
(binary not xml) your dataset and save in database.