C#中RichEdit控件,保存文本和圖片到mysql數據庫

分別經過內存流和RTF文件保存,我的感受是較爲完善的兩種方法,但願你們能夠用獲得,有什麼技術分享,歡迎下面留言數據庫

方法1:spa

//創建內存流
MemoryStream ms = new MemoryStream();
//ms.Position = 0;
//把當前的richtextbox內容包括圖片和文本保存到流中
richTextBox1.SaveFile(ms, RichTextBoxStreamType.RichText);
byte[] buffer = ms.GetBuffer();
// MySqlConnectString
MySqlConnection conn = new MySqlConnection(Properties.Settings.Default.MySqlConnectString);
string insertStr = "insert into httang_test.hxwang_richedit(image) values(@blobData);";//須要主鍵設置自增
MySqlParameter par = new MySqlParameter("@blobData", MySqlDbType.Blob);
par.Value = buffer;
MySqlCommand cmd = new MySqlCommand(insertStr, conn);
cmd.Parameters.Add(par);
try
{
conn.Open();
cmd.ExecuteNonQuery();
ms.Close();
ms.Dispose();
}
catch (Exception ep)
{
MessageBox.Show(ep.Message);
}
richTextBox1.Clear();
MessageBox.Show("成功插入數據庫!");圖片

}內存

方法2:cmd

if ((Path.GetExtension("D:\\text")).ToLower() == ".rtf")
richTextBox1.SaveFile("D:\\text", RichTextBoxStreamType.RichNoOleObjs);
else
richTextBox1.SaveFile("D:\\text");string

FileStream fs = new FileStream("D:\\text", FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] byData = br.ReadBytes((int)fs.Length);

//byte[] buffer = ms.GetBuffer();
//// MySqlConnectString
MySqlConnection conn = new MySqlConnection(Properties.Settings.Default.MySqlConnectString);
string insertStr = "insert into httang_test.hxwang_richedit(image) values(@blobData);";//須要主鍵設置自增
MySqlParameter par = new MySqlParameter("@blobData", MySqlDbType.Blob);
par.Value = byData;
MySqlCommand cmd = new MySqlCommand(insertStr, conn);
cmd.Parameters.Add(par);
try
{
conn.Open();
cmd.ExecuteNonQuery();
fs.Close();
fs.Dispose();
}
catch (Exception ep)
{
MessageBox.Show(ep.Message);
}
richTextBox1.Clear();
MessageBox.Show("成功插入數據庫!");it

}
}io

相關文章
相關標籤/搜索