Save the text in RichTextBox into SQL Server (C #) [Reserved]

zhaozj2021-02-16  87

Original address: http://www.developer.com/net/net/Article.php/11087_3373951_1

save:

private void btnSave_Click (object sender, System.EventArgs e) {FileStream stream = null; SqlConnection conn = null; SqlCommand cmd = null; try {richTextBox1.SaveFile ( "temp.rtf"); stream = new FileStream ( "temp.rtf ", FileMode.open, FileAccess.read; int size = convert.toint32 (stream.length); Byte [] RTF = new byte [size]; stream.read (RTF, 0, size);

Conn = new sqlconnection ("Database = northwind; integrated security = true;"); conn.open (); cmd = new sqlcommand ("Update Employees Set Photo = @photo where employeid = 1", conn);

SqlParameter paramRTF = new SqlParameter ( "@ Photo", SqlDbType.Image, rtf.Length, ParameterDirection.Input, false, 0,0, null, DataRowVersion.Current, rtf); cmd.Parameters.Add (paramRTF); int rowsUpdated = Convert.TOINT32 (cmd.executenonQuery ()); messagebox.show (String.Format ("{0} rows updated", rowsupdated);} catch (exception ex) {messagebox.show (ex. amount);} finally { IF (stream! = null) stream.close (); if (cmd! = null) cmd.parameters.clear (); if (conn! = null) conn.close ();}}

Read:

Private void btnload_click (object sender, system.eventargs e) {richtextbox1.clear ();

SqlConnection cn = null; SqlCommand cmd = null; SqlDataReader reader = null; try {cn = new SqlConnection ( "Database = Northwind; Integrated Security = true;"); cn.Open (); cmd = new SqlCommand ( "SELECT Photo FROM Employees where urgeyeid = 1 ", cn); reader = cmd.executeReader (); reader.read (); if (ready) {if (! Reader.isdbnull (0)) {byte [] RTF = New Byte [ Convert.TOINT32 ((Reader.getbytes (0, 0, NULL, 0, INT32.MaxValue))]; longcriptes (0, 0, RTF, 0, RTF.LENGTH); asciiencoding encoding = new asciiencoding (); Richtextbox1.rtf = encoding.getstring (RTF, 0, Convert.Toint 32 (BytesReceived));}}} Catch (Exception EX) {MessageBox.Show (EXMESSAGE);} finally {ix (Reader! = Null (); if (cn! = null) cn.close ();}}

转载请注明原文地址:https://www.9cbs.com/read-12825.html

New Post(0)