Storing data of rich text box to database with formatting

本秂侑毒 提交于 2019-11-27 10:02:34

问题


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!

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);
    }

回答1:


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.




回答2:


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:

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:

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.



来源:https://stackoverflow.com/questions/15983278/storing-data-of-rich-text-box-to-database-with-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!