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:
'/>