// Write the image to the database
Private void writeimage ()
{
Sqlcommand comm = conn. createcommand ();
Comm. commandtext = "insert into images (image, type) values (@ image, @ type )";
Comm. commandtype = commandtype. text;
Sqlparameter Param = comm. Parameters. Add ("@ image", sqldbtype. Image );
Param. value = readfile ();
Param = comm. Parameters. Add ("@ Type", sqldbtype. nvarchar );
Param. value = getcontenttype (New fileinfo (filename). extension. Remove (0, 1 ));
If(comm.exe cutenonquery () = 1)
Response. Write ("successful ");
Else
Response. Write ("fail ");
Conn. Close ();
}
// Read images from the database
Private void ReadImage ()
{
Sqlcommand comm = conn. createcommand ();
Comm. commandtext = "select image, type from images ";
Comm. commandtype = commandtype. text;
Sqldatareader reader = comm. executereader ();
While (reader. Read ())
{
Response. contenttype = reader ["type"]. tostring (); // The read/write type must be set. Otherwise, the browser willCompositionCurrent output
Response. binarywrite (byte []) Reader ["image"]); // picture data
}
Response. Write ("successful ");
Response. End ();
Conn. Close ();
}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////
// Other custom methods are as follows:
// Connect to the database
Private void connectdb ()
{
String connstr = "Initial catalog =; Data Source =; user id = sa; Password = ;";
Conn = new sqlconnection (connstr );
Conn. open ();
}
// get the file name
private string GetFile ()
{< br> httppostedfile file = file1.postedfile;
filename = file. filename;
return filename;
}< br> // read the file content
private byte [] readfile ()
{< br> filestream file = file. openread (GetFile ();
byte [] content = new byte;
file. read (content, 0, content. length);
file. close ();
return content;
}
// obtain the image suffix
private string getcontenttype (string extension)
{< br> string type = "";
If (extension. equals ("jpg") | extension. equals ("jpg")
type = "Jpeg";
else
type = extension;
return "image/" + type;
}