1. Create a test data table:
Create Table Test ([imageid] [int] identity (1, 1) not null, BMP image)
2. store image files in the database:
// Sqlconnection connection
String sqlstr = string. Format ("insert into test (BMP) values (@ I)", combobox1.text );
If (conn. State = connectionstate. Closed)
Conn. open ();
If (conn. State = connectionstate. open)
{
Sqlcommand cmd = new sqlcommand (sqlstr, Conn );
Filestream stream = new filestream ("1.png", filemode. Open );
Int Len = (INT) stream. length;
Byte [] bytes = new byte [Len];
Stream. Read (bytes, 0, Len );
Cmd. Parameters. Add ("@ I", sqldbtype. Image, Len );
Cmd. Parameters ["@ I"]. value = bytes;
Cmd. executenonquery ();
Stream. Close ();
}
Else
{
MessageBox. Show ("failed to open database connection .");
}
Conn. Close ();
3. Read the image from the database and display it in picturebox:
Bindingsource source = new bindingsource ();
Source. positionchanged + = new eventhandler (source_positionchanged );
// create a bindingnavigator data navigation control on the form. When a data table is opened and moved between data items, the following event method is triggered:
void source_positionchanged (Object sender, eventargs e)
{< br> byte [] image_bytes = (byte []) (datarowview) source. current ). row ["BMP"];
memorystream stream = new memorystream (image_bytes);
picturebox1.image = image. fromstream (Stream);
stream. close ();
}