Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用格式将富文本框数据存储到数据库_C#_Mysql_Wpf_Database_Richtextbox - Fatal编程技术网

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

C# 用格式将富文本框数据存储到数据库,c#,mysql,wpf,database,richtextbox,C#,Mysql,Wpf,Database,Richtextbox,我是wpf的新手,我想将富文本框的数据及其格式(斜体、彩色、粗体…)存储到数据库(Mysql)中。 当前,当我保存数据时,格式设置被忽略。 此外,当我从数据库将文本加载回富文本框时,它会在同一行中显示所有文本。 期待您的帮助和建议 public void save() { MySqlConnection conn = new MySqlConnection(connString); MySqlCommand command = conn.Create

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

试着这样做:

RichTextBox richTextBox = new RichTextBox();
string richText = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text;
string query = "INSERT INTO blah VALUES ('" + HTTPUtility.HtmlEncode(richText) +  "');
然后,在将其保存到MySQL时,您可以按如下方式构建查询:

RichTextBox richTextBox = new RichTextBox();
string richText = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text;
string query = "INSERT INTO blah VALUES ('" + HTTPUtility.HtmlEncode(richText) +  "');
这将确保您的内容保持正确的格式

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

HTTPUtility.HtmlDecode(selectedDataFromMySQL);
或者更全面地说:

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

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

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

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。

希望帮助STHX获得答案,但即使我添加了system.webI,当前上下文中也不存在“HttpUtility”。我已添加了对system.Web的引用,并使用了system.Web,但仍然不起作用。请确保您的项目没有针对项目属性中的某个客户端配置文件框架。I已经选中,我的目标是.Net framework 4.5(而不是客户端配置文件),因为您使用的是.Net 4.5,您可以替换掉HTTPUtility.HtmlEncode/Decode,因为:Dosent work亲爱的,显示richtextbox没有任何称为Document的定义。@SharadAg。这是针对WPF的,而不是WinForms。如果您收到错误“对非共享成员的引用需要对象引用”。请注意,他使用的是“richTextBox”作为变量,而不是类。这意味着您需要以下代码来允许TextRange工作。”RichTextBox RichTextBox=默认值(RichTextBox);'伟大的解决方案,顺便说一句,竖起大拇指。