RichEdit controls in C #, saving text and pictures to MySQL database

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.