【问题标题】:Storing data of rich text box to database with formatting使用格式化将富文本框的数据存储到数据库
【发布时间】:2013-04-13 01:42:15
【问题描述】:

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

【问题讨论】:

  • 希望this有帮助

标签: c# mysql wpf database richtextbox


【解决方案1】:

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

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

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

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

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

【讨论】:

  • Dosent 工作亲爱的,显示 Richtextbox 没有任何名为 Document 的定义。
  • @SharadAg。这是针对 WPF,而不是 WinForms。
  • 如果您收到错误“对非共享成员的引用需要对象引用。”请注意,他使用“richTextBox”作为变量而不是类。这意味着您需要以下代码才能使 TextRange 工作。 '富文本框富文本框 = 默认(富文本框);'顺便说一句,很棒的解决方案,竖起大拇指。
【解决方案2】:

试试这样的:

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

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

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

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

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

HTTPUtility.HtmlDecode(selectedDataFromMySQL);

或者,更完整地说:

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

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

【讨论】:

  • 感谢答案,但即使我添加 system.web,当前上下文中也不存在“HttpUtility”
  • 我添加了对 System.Web 的引用并使用 System.Web 但仍然无法正常工作
  • 确保您的项目未针对项目属性中的客户端配置文件框架之一。
  • 我已经检查过了,我的目标是 .Net framework 4.5(不是客户端配置文件)
  • 由于您使用的是 .NET 4.5,因此您可以为此更换 HTTPUtility.HtmlEncode/Decode:msdn.microsoft.com/en-us/library/…
【解决方案3】:

对于 WinForm 应用程序,使用richTextBox1.rtf,而不是richTextBox.Text

【讨论】:

  • 此线程适用于 WPF。因此,您在这里的答案超出了线程范围。但是,如果您共享了 WinForms 的可行替代方案,您的答案可能是“有效的”。这将有助于人们寻找 WinForms 解决方案。
猜你喜欢
  • 2013-04-05
  • 2020-12-07
  • 1970-01-01
  • 2015-04-25
  • 2013-12-15
  • 2011-07-26
  • 1970-01-01
  • 2017-05-07
  • 1970-01-01
相关资源
最近更新 更多