// Store
Pirvate save ()
{
Httppostedfile upfile = up_file.postedfile; // The httppostedfile object, which is used to read the attributes of the uploaded image.
Filelength = upfile. contentlength; // record file length
Try
{
If (filelength = 0) // when the file length is 0
{
Txtmessage. Text = "select the file to upload! ";
}
Else
{
Byte [] filebyte = new byte [filelength]; // use the image length to initialize a byte array to store temporary image files
Stream filestream = upfile. inputstream; // create a file stream object
Filestream. Read (filebyte, 0, filelength); // read the image data to the temporary storage object filebyte. 0 indicates the Data Pointer position, and filelength indicates the Data Length.
String connstring = "Data Source = 192.168.1.250; database = image; uid = pwqzc; Pwd = cn0088 ";
Sqlconnection conn = new sqlconnection (connstring); // initialize the database connection
String insertstr = "insert into image (image_data, image_content_type, image_description, image_size) values (@ image_data, @ image_content_type, @ image_description, @ image_size )";
// Insert a database statement
Sqlcommand comm = new sqlcommand (insertstr, Conn );
Comm. Parameters. Add (New sqlparameter ("@ image_data", sqldbtype. Image); // Add a parameter
Comm. Parameters ["@ image_data"]. value = filebyte; // assign a value to the parameter
Comm. Parameters. Add (New sqlparameter ("@ image_content_type", sqldbtype. varchar, 50 ));
Comm. Parameters ["@ image_content_type"]. value = upfile. contenttype; // record the image type
Comm. Parameters. Add (New sqlparameter ("@ image_description", sqldbtype. varchar, 50 ));
Comm. Parameters ["@ image_description"]. value = txtdescription. Text; // upload other form data
Comm. Parameters. Add (New sqlparameter ("@ image_size", sqldbtype. Int, 4 ));
Comm. Parameters ["@ image_size"]. value = upfile. contentlength; // record the image length, which is used to read data
Conn. open (); // open the database connection
Comm. executenonquery (); // Add data
Conn. Close (); // close the database
Txtmessage. Text = "you have successfully uploaded an image ";
}
}
Catch (exception ex)
{
Txtmessage. Text = ex. Message. tostring ();
}
}
// Read
Pirvate read ()
{
Int imgid = int. parse (request. querystring ["imgid"]); // imgid indicates the image ID.
// Establish a database connection
String connstring = "Server = 192.168.1.250; database = image; uid = pwqzc; Pwd = cn0088 ";
Sqlconnection conn = new sqlconnection (connstring );
// Database operation statement
String selstring = "select * from image where image_id = @ image_id ";
Sqlcommand comm = new sqlcommand (selstring, Conn );
Comm. Parameters. Add (New sqlparameter ("@ image_id", sqldbtype. Int, 4 ));
Comm. Parameters ["@ image_id"]. value = imgid;
Conn. open (); // open the database connection
Sqldatareader DR = comm. executereader (); // read data
Dr. Read (); // read a row
// Set the output file type
Response. contenttype = (string) Dr ["image_content_type"];
// Output the binary data of the image file
Response. outputstream. Write (byte []) Dr ["image_data"], 0, (INT) Dr ["image_size"]);
Response. End ();
Dr. Close ();
Conn. Close ();
}