Save the image to the SQL Server database

Source: Internet
Author: User
Create an image storage database table in SQL Server. imagedata column is the binary data storage field of the image, and imagecontenttype column is the record field of the image file type, imagedescription column is the description field of the saved image file, and imagesize column is the length field of the saved image file. The structure is as follows:
Create Table [DBO]. [imagestore] (
[Imageid] [int] identity (1, 1) not null,
[Imagedata] [Image] Null,
[Imagecontenttype] [varchar] (50) Collate chinese_prc_ci_as null,
[Imagedescription] [varchar] (200) Collate chinese_prc_ci_as null,
[Imagesize] [int] Null
) On [primary] textimage_on [primary]
*/

// Uploadimage. aspx Program The content is as follows:
<% @ Page inherits = "uploadimage. uploadimage" src = "uploadimage. cs" Language = "C #" %>
<HTML> <title> upload an image </title>
<Body bgcolor = "# ffffff">
<Form enctype = "multipart/form-Data" runat = "server" id = "form1">
<Table runat = "server" width = "700" align = "Left" id = "Table1" cellpadding = "0" cellspacing = "0" border = "0">
<Tr>
& Nbsp; <TD> upload an image (select the image you want to upload) </TD>
<TD>
<Input type = "file" id = "up_file" runat = "server" style = "width: 320" Accept = "text/*" name = "up_file">
</TD>
</Tr>
<Tr>
& Nbsp; <TD>
File description (add upload image description, such as author and source)
& Nbsp; </TD>
<TD>
<Asp: textbox runat = "server" width = "239" id = "txtdescription" maintainstate = "false"/>
</TD>
</Tr>
<Tr>
<TD>
<Asp: Label runat = "server" id = "txtmessage" forecolor = "red" maintainstate = "false"/>
</TD>
<TD>
<Asp: button runat = "server" width = "239" onclick = "button_submit" text = "Upload image"/>
</TD>
</Tr>
</Table>
</Form>
</Body>
</Html>
//-------------------------------------------------------------------
// The content of the uploadimage. CS program is as follows:
Using system;
Using system. Web;
Using system. IO;
Using system. Data;
Using system. Data. sqlclient;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Namespace uploadimage
{
Public class uploadimage: Page {
Protected htmlinputfile up_file; // htmlcontrol, webcontrols control object
Protected textbox txtdescription;
Protected label txtmessage;
Protected int32 filelength = 0; // record file length variable
Protected void button_submit (system. Object sender, system. eventargs e ){
Httppostedfile upfile = up_file.postedfile; & nbsp; // httppostedfile object, used to read image file attributes
Filelength = upfile. contentlength; // record file length
Try {
If (filelength = 0) {// The file length is zero
Txtmessage. Text = "<B> select the file you want to upload </B> ";
} Else {
Byte [] filebytearray = new byte [filelength]; // temporary storage of byte Arrays for image files
Stream streamobject = upfile. inputstream; // create a data stream object
// Read image file data. filebytearray is the data storage body, 0 is the Data Pointer position, and filelne is the data length.
Streamobject. Read (filebytearray, 0, filelength );
// Create an SQL Server Link
Sqlconnection con = new sqlconnection ("Data Source = localhost; initial catalog = testdb; user id = sa; Pwd = ;");
String sqlcmd = "insert into imagestore (imagedata, imagecontenttype, imagedescription, imagesize) values (@ image, @ contenttype, @ imagedescription, @ imagesize )";
Sqlcommand cmdobj = new sqlcommand (sqlcmd, con );
Cmdobj. Parameters. Add ("@ image", sqldbtype. Binary, filelength). value = filebytearray;
Cmdobj. Parameters. Add ("@ contenttype", sqldbtype. varchar, 50). value = upfile. contenttype; & nbsp; // record file type
// Upload other single table data records
Cmdobj. Parameters. Add ("@ imagedescription", sqldbtype. varchar, 200). value = txtdescription. text;
// Record the file length, which is used for reading
Cmdobj. Parameters. Add ("@ imagesize", sqldbtype. bigint, 8). value = upfile. contentlength;
Con. open ();
Cmdobj. executenonquery ();
Con. Close ();
Txtmessage. Text = "<p> <B> OK! You have successfully uploaded your image </B> "; // a message indicating that the image has been uploaded successfully
}
} Catch (exception ex ){
Txtmessage. Text = ex. Message. tostring ();
}}}}
//----------------------------------------------------------------------
// Well, the image has been uploaded to the database. What should I do now? Of course, it is read from the database and displayed on the web page. Please refer to the following program:
// The ReadImage. aspx program content is as follows:
/-----------------------------------------------------------------------
<% @ Page inherits = "ReadImage. maindisplay" src = "ReadImage. cs" %>
//----------------------------------------------------------------------
// The ReadImage. CS program content is as follows:
Using system;
Using system. Data;
Using system. Data. sqlclient;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Namespace ReadImage {
Public class maindisplay: system. Web. UI. Page {
Public void page_load (system. Object sender, system. eventargs e ){
Int imgid = convert. toint32 (request. querystring ["imgid"]); & nbsp; // imgid is the image ID
// Create a database link
Sqlconnection con = new sqlconnection ("Data Source = King; initial catalog = testdb; user id = sa; Pwd = ;");
String sqlcmd = "select * From imagestore where imageid = @ imageid ";
Sqlcommand cmdobj = new sqlcommand (sqlcmd, con );
Cmdobj. Parameters. Add ("@ imageid", sqldbtype. INT). value = imgid;
Con. open ();
Sqldatareader sqlreader = cmdobj. executereader ();
Sqlreader. Read ();
Response. contenttype = (string) sqlreader ["imagecontenttype"]; // set the output file type
// Binary number of output image files
Response. outputstream. Write (byte []) sqlreader ["imagedata"], 0, (INT) sqlreader ["imagesize"]);
Response. End ();
Con. Close ();
// Very simple. ^_^
& Nbsp ;}
}
}
//--------------------------------------------------------------------
// Finally, we must display it on the web page.
// Showimage. hml
<HTML>
<Body>
This is the image read from the database:
<Body>
</Html>
//------------------------------------------------------------------
// At last, there are still many improvements to this program. I hope you can write more Image Upload programs for multi-encoding.
// Good luck, Engine
---------------------------------------------------------------

Go to www.chinabs.net, where there is Source code Download!
---------------------------------------------------------------

Byte [] filebytearray = new byte [filelength];
Defines an array of the filelength and byte type.
Dim filebyetarray (filelength) as byte

Up_file is the ID of the file field.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.