ASP. NET access SQL Server database image field data

Source: Internet
Author: User
Tags connectionstrings

Generally, the image path is saved in the database. However, in some cases, the image field is used, which involves the image Field Data Reading Technology in the SQL Server database.

1. Store the image field data

If (fileupload1.hasfile)
{
// Note: valid in IE. The file types in different browsers are different.

If (this. fileupload1.postedfile. contenttype = "image/BMP" |
This. fileupload1.postedfile. contenttype = "image/pjpeg" |
This. fileupload1.postedfile. contenttype = "image/GIF" |
This. fileupload1.postedfile. contenttype = "image/X-PNG ")
{
Int bookid = convert. toint32 (textbox1.text );
Byte [] bookimg = fileupload1.filebytes;
Using (sqlconnection connection = new sqlconnection (configurationmanager. connectionstrings ["librarymsconnectionstring"]. connectionstring ))
{
Using (sqlcommand command = new sqlcommand ("insert into bookimage values (@ bookid, @ bookimg)", connection ))
{

Command. Parameters. Add (New sqlparameter ("@ bookid", bookid ));

Command. Parameters. Add (New sqlparameter ("@ bookimg", bookimg ));
Connection. open ();
Command. executenonquery ();
}
}
}

}

2. Read and display the image field data

2.1 read with a general Handler

Public class handler: ihttphandler
{

Public bool isreusable
{
Get
{
Return false;
}
}

Public void processrequest (httpcontext context)
{
Context. response. contenttype = "image/JPEG/GIF/X-PNG ";
Context. response. cache. setcacheability (httpcacheability. Public );
Context. response. bufferoutput = false;

Int32 id =-1;
Stream stream = NULL;

If (context. Request. querystring ["bookid"]! = NULL & context. Request. querystring ["bookid"]! = "")
{
Id = convert. toint32 (context. Request. querystring ["bookid"]);

Stream = bookmanager. getimage (ID );

If (stream! = NULL)
{
Const int buffersize = 1024*16;
Byte [] buffer = new byte [buffersize];
Int COUNT = stream. Read (buffer, 0, buffersize );
While (count> 0)
{
Context. response. outputstream. Write (buffer, 0, count );
Count = stream. Read (buffer, 0, buffersize );
}
}
}

}
}

Getimage method:

Public static stream getimage (INT bookid)
{
Using (sqlconnection connection = new sqlconnection (configurationmanager. connectionstrings ["librarymsconnectionstring"]. connectionstring ))
{
Using (sqlcommand command = new sqlcommand ("select bookimg from bookimage where bookid = @ bookid", connection ))
{
Command. commandtype = commandtype. text;
Command. Parameters. Add (New sqlparameter ("@ bookid", bookid ));
Connection. open ();
Object result = command. executescalar ();
Try
{
Return new memorystream (byte []) result );
}
Catch
{
Return NULL;
}
}
}

}

You can also use the following code:

Public void processrequest (httpcontext context)
{
Context. response. contenttype = "image/JPEG/GIF/X-PNG ";
Context. response. cache. setcacheability (httpcacheability. Public );
Context. response. bufferoutput = false;

Int32 id =-1;
Stream stream = NULL;

If (context. Request. querystring ["bookid"]! = NULL & context. Request. querystring ["bookid"]! = "")
{
Id = convert. toint32 (context. Request. querystring ["bookid"]);
Byte [] file = (byte []) getimage (ID); // obtain the image information
Context. response. binarywrite (File );
}

}
Public static byte [] getimage (INT bookid)
{
Using (sqlconnection connection = new sqlconnection (system. configuration. configurationmanager. connectionstrings ["librarymsconnectionstring"]. connectionstring ))
{
Using (sqlcommand command = new sqlcommand ("select bookimg from bookimage where bookid = @ bookid", connection ))
{
Command. Parameters. Add (New sqlparameter ("@ bookid", bookid ));
Connection. open ();
Sqldatareader result = command. executereader ();
If (result. Read ())
{
Try
{
Return (byte []) result [0];
}
Catch
{
Return NULL;
}
}
Else
Return NULL;
}
}
}

2.2 page display

If the image control is used, the code is:

Image1.imageurl = "handler. ashx? Bookid = "+ bookid;

If the image label is used:

'/>

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.