Respectively through the memory stream and the RTF file preservation, personal feeling is a more perfect two methods, I hope you can use to get, what technology to share, welcome the following message
Method 1:
Establish a memory stream
MemoryStream ms = new MemoryStream ();
Ms. Position = 0;
Save current RichTextBox content including pictures and text to the stream
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);"; /need primary key set self-increment
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 ("Insert database successfully! ");
}
Method 2:
If ((Path.getextension ("D:\\text")). ToLower () = = ". rtf")
Richtextbox1.savefile ("D:\\text", RICHTEXTBOXSTREAMTYPE.RICHNOOLEOBJS);
Else
Richtextbox1.savefile ("D:\\text");
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);"; /need primary key set self-increment
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 ("Insert database successfully! ");
}
}
RichEdit controls in C #, saving text and pictures to MySQL database