Read from database:
Method 1:
Sqlcommand cmd = new sqlcommand ("select * from student", mycn );
Sqldataadapter da = new sqldataadapter (CMD );
Dataset DS = new dataset ();
Da. Fill (DS, "student ");
Byte [] byteblobdata = new byte [0];
Byteblobdata = (byte []) (Ds. Tables ["student"]. Rows [Index] ["ZP"]);
Memorystream bytes blobdata = new memorystream (byteblobdata );
This. picturebox2.image = image. fromstream (describlobdata );
Method 2:
Oledbdataadapter myda = new oledbdataadapter ("select ZP from picture", mycn );
Dataset DS = new dataset ();
Myda. Fill (DS, "picture ");
Foreach (datarow rfirst in DS. Tables ["picture"]. Rows)
{
Bdata = (byte []) rfirst ["ZP"];
}
Memorystream stream = new memorystream (bdata, true );
// Memorystream stream = new memorystream (bdata );
// Create a bitmap from the stream
Bitmap BMP = new Bitmap (Stream );
Try
{
// Showing
This. picturebox1.image = BMP;
// This. picturebox1.image = image. fromstream (Stream );
}
Catch {};
// Close the stream
Stream. Close ();
========================================================== ======================================
Insert to database:
Sqlcommand mycmd = new sqlcommand ("insert into student (XH, ZP) values (@ XH, @ ZP)", mycn );
// Read JPG into file stream, and from there into byte array.
Filestream fsblobfile = new filestream (strblobfilepath, filemode. Open, fileaccess. Read );
Byte [] bytblobdata = new byte [fsblobfile. Length];
Fsblobfile. Read (bytblobdata, 0, bytblobdata. Length );
Fsblobfile. Close ();
Mycmd. Parameters. Add ("@ XH", this.txt box_xh.text.trim ());
// Create parameter for INSERT command and add to sqlcommand object.
Sqlparameter PRM = new sqlparameter ("@ ZP", sqldbtype. varbinary, bytblobdata. length, parameterdirection. Input, false, 0, 0, null, datarowversion. Current, bytblobdata );
Mycmd. Parameters. Add (PRM );
try
{< br> mycn. open ();
mycmd. executenonquery ();
MessageBox. show ("saved to the database! "," Prompt ");
}< br> catch (exception ex)
{< br> MessageBox. show (ex. message);
}< br> mycn. close ();