C# 使用格式将富文本框的数据存储到数据库中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15983278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:40:17  来源:igfitidea点击:

Storing data of rich text box to database with formatting

c#mysqlwpfdatabaserichtextbox

提问by Amine Da.

I am new at wpf and I want to store the data of the rich text box along with its formatting (Italic, colored, Bold..) into a database (Mysql). currently when i save the data, formatting is ignored. in addition, it shows all the text in the same line when i load it back to the rich text box from the database. Looking forward to your help and suggestions!

我是 wpf 的新手,我想将富文本框的数据及其格式(斜体、彩色、粗体……)存储到数据库(Mysql)中。目前,当我保存数据时,格式被忽略。此外,当我将它从数据库加载回富文本框时,它会在同一行中显示所有文本。期待您的帮助和建议!

public void save()
    {  

        MySqlConnection conn = new MySqlConnection(connString);
        MySqlCommand command = conn.CreateCommand();      
        string richText = new TextRange(rt1.Document.ContentStart,  rt1.Document.ContentEnd).Text;

        string s = WebUtility.HtmlEncode(richText); 
        command.Parameters.AddWithValue("@s", s);           
        command.CommandText = "insert into proc_tra (procedures) values (@s)";
        conn.Open();
        command.ExecuteNonQuery();
        conn.Close();
    }

public void load()

    {   MySqlConnection conn = new MySqlConnection(connString);
        MySqlCommand command = conn.CreateCommand();
        command.CommandText = "select * from proc_tra where id_pt=4";
        rt1.Document.Blocks.Clear();            
        conn.Open();            
        MySqlDataReader dr;
        dr = command.ExecuteReader();
        string k="";           
        while (dr.Read())
        {              
            k += dr["procedures"].ToString();
        }
        var p = new Paragraph();
        var run = new Run();
        run.Text = WebUtility.HtmlDecode(k);
        p.Inlines.Add(run);
        rt1.Document.Blocks.Add(p);
    }

采纳答案by buylar

To get the formatted text that will be saved in the db:

要获取将保存在数据库中的格式化文本:

string rtfText; //string to save to db
TextRange tr = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
using (MemoryStream ms = new MemoryStream())
{
    tr.Save(ms, DataFormats.Rtf);
    rtfText = Encoding.ASCII.GetString(ms.ToArray());
}

To restore the formatted text retrieved from the db:

要恢复从数据库中检索到的格式化文本:

string rtfText= ... //string from db
byte[] byteArray = Encoding.ASCII.GetBytes(rtfText);
using (MemoryStream ms = new MemoryStream(byteArray))
{
    TextRange tr = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
    tr.Load(ms, DataFormats.Rtf);
}

You can also use XAML format instead, using DataFormats.XAML on load an save.

您也可以改用 XAML 格式,在加载保存时使用 DataFormats.XAML。

回答by Timothy Randall

Try something like this:

尝试这样的事情:

RichTextBox richTextBox = new RichTextBox();
string richText = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text;

Then, when going to save it to MySQL, you can build your query like this:

然后,在将其保存到 MySQL 时,您可以像这样构建查询:

string query = "INSERT INTO blah VALUES ('" + HTTPUtility.HtmlEncode(richText) +  "');

This will ensure that your content stays formatted correctly.

这将确保您的内容保持正确格式。

Finally when you perform your select to load the content back into the RichTextBox take the string you get and use:

最后,当您执行选择以将内容加载回 RichTextBox 时,获取并使用您获得的字符串:

HTTPUtility.HtmlDecode(selectedDataFromMySQL);

or, more completely:

或者,更完整地:

richTextBox.Document.Blocks.Clear();
richTextBox.Document.Blocks.Add(new Paragraph(HTTPUtility.HtmlDecode(selectedDataFromMySQL);

While I haven't done this in a while myself, I believe there is an extension for the WPF and the control that includes a Text property so that may prove useful as well.

虽然我自己已经有一段时间没有这样做了,但我相信 WPF 和包含 Text 属性的控件有一个扩展,因此它也可能很有用。