mysql 将富文本框中的数据格式化存储到数据库中

8nuwlpux  于 2022-12-10  发布在  Mysql
关注(0)|答案(4)|浏览(521)

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

hi3rlvi21#

要获取将保存在数据库中的格式化文本,请执行以下操作:

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。

vatpfxk5

vatpfxk52#

试着这样做:

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) +  "');

这将确保您的内容保持正确的格式。
最后,当您执行SELECT以将内容加载回RichTextBox时,获取您获得的字符串并使用:

HTTPUtility.HtmlDecode(selectedDataFromMySQL);

或者,更完整地说:

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

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

ie3xauqp

ie3xauqp3#

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

zfycwa2u

zfycwa2u4#

公共函数BacklashInsert(ByVal mrtfdat as字符串)为字符串

'insert / into data to keep format/ in mysql record
    Dim n As Integer = mrtfdat.Length
    Dim nrtf As String = ""
    Dim mchrasc As Integer = 0
    For i As Integer = 0 To n - 1
        mchrasc = Asc(mrtfdat.Substring(i))
        If mchrasc = 92 Then
            nrtf = nrtf & Chr(42)
        Else
            nrtf = nrtf & Chr(mchrasc)
        End If
    Next
    Return nrtf
End Function
Public Function Backlaskrip(ByVal mrtfdat As String) As String
    Dim n As Integer = mrtfdat.Length
    Dim nrtf As String = ""
    Dim mchrasc As Integer = 0
    For i As Integer = 0 To n - 1
        mchrasc = Asc(mrtfdat.Substring(i))
        If mchrasc = 42 Then
            nrtf = nrtf & Chr(92)
        Else
            nrtf = nrtf & Chr(mchrasc)
        End If
    Next
    Return nrtf
End Function

我是用VB写的,这是我用的代码

相关问题