将 FlowDocument 保存到 SQL Server
我需要将 WPF FlowDocuments 保存到 SQL Server。这样做的最佳格式是什么?细绳?斑点?这对于少于 5K 字左右的文档有影响吗?
I need to save WPF FlowDocuments to SQL Server. What is the best format for doing that? String? Blob? Does it matter in a document less than 5K words or so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
FlowDocument 不可序列化,因此 SWeko 的上述答案将不起作用。
您可以使用以下方法从 Xaml 字符串获取 FlowDocument,然后可以使用 nvarchar(max) 将其保存在数据库中。
和
FlowDocument is not serializable so SWeko's answer above will not work.
You can use the methods below to get the FlowDocument to and from a Xaml string which can then be saved in the database using nvarchar(max).
and
如果您只想将 FlowDocument 对象存储在数据库中,而不进行任何处理,我建议使用二进制序列化,并将生成的字节数组存储到 varbinary(max) 中。这是快速且可扩展的。
但是,如果您已经将 FlowDocuments 作为 XML 文件,那么将它们转储到 nvarchar(max) 字段中会更容易,而无需(添加)序列化/反序列化开销。对于 8k 以下的值,这可以轻松扩展,然后执行得还不错,直到达到 10MB 左右。
If you just want to store the FlowDocument objects in a database, without any processing, I would recommend using binary serialization, and storing the resulting byte array into a varbinary(max). This is fast and scales well.
However, if you already have the FlowDocuments as XML files, than it would be easier just to dump them into a nvarchar(max) field, with no (added) serialization/deserialization overhead. This scales trivially for values under 8k, and then performs kinda OK until you hit around the 10MB mark.
您可以使用 TextRange 类序列化 FlowDocument。您甚至可以使用 RTF 格式。保存:
和加载:
另请参阅 https://www.wpf-tutorial.com/rich-text-controls/how-to-creating-a-rich-text-editor/
You can serialize FlowDocument using the TextRange class. You can even use the RTF format. Saving:
And loading:
See also https://www.wpf-tutorial.com/rich-text-controls/how-to-creating-a-rich-text-editor/