如何将 RichTextBox 内容保存到 SQL varbinary(字节数组)列中?

发布于 2024-08-08 13:46:36 字数 906 浏览 1 评论 0原文

我想以 XamlPackage 格式将 RichTextBox 的内容保存到 varbinary (= 字节数组)。 我需要有关如何操作的技术建议。

我实际上需要知道如何在 FlowDocument 和字节数组之间进行转换。

是否甚至建议将其存储为 varbinary,或者这是一个坏主意?


更新

代码片段:

///Load
byte[] document = GetDocumentFromDataBase();
RickTextBox tb = new RickTextBox();
TextRange tr = new TextRange(tb.Document.ContentStart, tb.Document.ContentEnd)
tr.Load(--------------------------) //Load from the byte array.


///Save
int maxAllowed = 1024;
byte[] document;
RichTextBox tb = new RichTextBox();
//User entered text and designs in the rich text
TextRange tr = new TextRange(tb.Document.ContentStart, tb.Document.ContentEnd)   
tr.Save(--------------------------) //Save to byte array
if (document.Length > maxAllowed) 
{
    MessageBox.Show((document.Length - maxAllowed) + " Exceeding limit.");
    return;
}
SaveToDataBase();
TextRange

I want to save content of a RichTextBox to varbinary (= byte array) in XamlPackage format.
I need technicial advise on how to it.

I actually need to know how to convert between FlowDocument to byte array.

Is it even recommended to store it as varbinary, or this is a bad idea?


Update

Code snippet:

///Load
byte[] document = GetDocumentFromDataBase();
RickTextBox tb = new RickTextBox();
TextRange tr = new TextRange(tb.Document.ContentStart, tb.Document.ContentEnd)
tr.Load(--------------------------) //Load from the byte array.


///Save
int maxAllowed = 1024;
byte[] document;
RichTextBox tb = new RichTextBox();
//User entered text and designs in the rich text
TextRange tr = new TextRange(tb.Document.ContentStart, tb.Document.ContentEnd)   
tr.Save(--------------------------) //Save to byte array
if (document.Length > maxAllowed) 
{
    MessageBox.Show((document.Length - maxAllowed) + " Exceeding limit.");
    return;
}
SaveToDataBase();
TextRange

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

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

发布评论

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

评论(1

黒涩兲箜 2024-08-15 13:46:36

我现在找不到完整的示例,但您可以使用 XamlReader 和 XamlWriter 将文档放入和取出字符串。从那里,您可以使用 UnicodeEncoding、AsciiEncoding 或任何您想要将其输入和输出字节的编码器。

我从字符串设置文档的简短示例......
docReader 是我的流程文档阅读器

        private void SetDetails(string detailsString)
    {
        if (docReader == null)
            return;
        if (String.IsNullOrEmpty(detailsString))
        {
            this.docReader.Document = null;
            return;
        }
        using (
        StringReader stringReader = new StringReader(detailsString))
        {
            using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(stringReader))
            {
                this.docReader.Document = XamlReader.Load(reader) as FlowDocument;
            }
        }
    }

I can't find my full example right now, but you can use XamlReader and XamlWriter to get the document into and out of a string. From there, you can use UnicodeEncoding, AsciiEncoding or whatever encoder you want to get it into and out of bytes.

My shorter example for setting the document from a string...
docReader is my flow document reader

        private void SetDetails(string detailsString)
    {
        if (docReader == null)
            return;
        if (String.IsNullOrEmpty(detailsString))
        {
            this.docReader.Document = null;
            return;
        }
        using (
        StringReader stringReader = new StringReader(detailsString))
        {
            using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(stringReader))
            {
                this.docReader.Document = XamlReader.Load(reader) as FlowDocument;
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文