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的新手,我想将富文本框的数据及其格式(Italic,Colored,Bold ..)存储到数据库(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);
}
2 个解决方案
#1
20
To get the formatted text that will be saved in the db:
要获取将保存在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:
要恢复从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加载保存。
#2
0
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:
最后,当您执行select以将内容加载回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属性的控件有一个扩展,因此也可能有用。
#1
20
To get the formatted text that will be saved in the db:
要获取将保存在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:
要恢复从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加载保存。
#2
0
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:
最后,当您执行select以将内容加载回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属性的控件有一个扩展,因此也可能有用。