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 employeeid = 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. Message );
}
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 employeeid = 1", CN );
Reader = cmd. executereader ();
Reader. Read ();
If (reader. hasrows)
...{
If (! Reader. isdbnull (0 ))
...{
Byte [] RTF = new byte [convert. toint32 (reader. getbytes (0, 0, null, 0, int32.maxvalue)];
Long bytesreceived = reader. getbytes (0, 0, RTF, 0, RTF. Length );
Asciiencoding encoding = new asciiencoding ();
Richtextbox1.rtf = encoding. getstring (RTF, 0, convert. toint32 (bytesreceived ));
}
}
}
Catch (exception ex)
...{
MessageBox. Show (ex. Message );
}
Finally
...{
If (reader! = NULL) reader. Close ();
If (CN! = NULL) Cn. Close ();
}