Storage and display of binary databases with images

Source: Internet
Author: User

Storage and display of binary databases with images
1. store images in binary format to the database.
2. Read Binary Images and display them on the page
3. Set the image control to display the binary image read from the database.
4. The imagefield in the gridview displays the image in URL mode.
5. display the Read Binary Image in the gridview
================================

1. store images in binary format to the database.

C #
--------------------------
// Save the image to the database
Protected void button#click (Object sender, eventargs E)
{
// Image path
String strpath = "~ /Photo/03.jpg ";
String strphotopath = server. mappath (strpath );
// Read the image
Filestream FS = new system. Io. filestream (strphotopath, filemode. Open, fileaccess. Read );
Binaryreader BR = new binaryreader (FS );
Byte [] photo = Br. readbytes (INT) fs. Length );
BR. Close ();
FS. Close ();
// Save
Sqlconnection myconn = new sqlconnection ("Data Source = 127.0.0.1; initial catalog = testdb; user id = sa; Password = sa ");
String strcomm = "insert into personphoto (personname, personphotopath, personphoto )";
Strcomm + = "values ('wangwu', '" + strpath + "', @ photobinary )";
Sqlcommand mycomm = new sqlcommand (strcomm, myconn );
Mycomm. Parameters. Add ("@ photobinary", sqldbtype. Binary, photo. Length );
Mycomm. Parameters ["@ photobinary"]. value = photo;
Myconn. open ();
Mycomm. executenonquery ();
Myconn. Close ();
}

VB. NET

'Save the image to the database

Protected sub button#click (byval sender as object, byval e as eventargs)
'Image path
Dim strpath as string = "~ /Photo/03.jpg"
Dim strphotopath as string = server. mappath (strpath)
'Reading Images
Dim FS as filestream = new system. Io. filestream (strphotopath, filemode. Open, fileaccess. Read)
Dim BR as binaryreader = new binaryreader (FS)
Dim photo () as byte = Br. readbytes (ctype (FS. length, integer ))
BR. Close
FS. Close
'Save
Dim myconn as sqlconnection = new sqlconnection ("Data Source = 127.0.0.1; initial catalog = testdb; user id = sa; Password = sa ")
Dim strcomm as string = "insert into personphoto (personname, personphotopath, personphoto )"
Strcomm = (strcomm + ("values ('wangw ','"_
+ (Strpath + "', @ photobinary )")))
Dim mycomm as sqlcommand = new sqlcommand (strcomm, myconn)
Mycomm. Parameters. Add ("@ photobinary", sqldbtype. Binary, photo. length)
Mycomm. parameters ("@ photobinary"). value = photo
Myconn. Open
Mycomm. executenonquery
Myconn. Close
End sub

2. Read Binary Images and display them on the page

C #
--------------------------
// Read the image
Sqlconnection myconn = new sqlconnection ("Data Source = 127.0.0.1; initial catalog = testdb; user id = sa; Password = sa ");
String strcomm = "select personphoto from personphoto where personname = 'wangwu '";
Sqlcommand mycomm = new sqlcommand (strcomm, myconn );
Myconn. open ();
Sqldatareader DR = mycomm. executereader ();
While (dr. Read ())
{
Byte [] photo = (byte []) Dr ["personphoto"];
This. response. binarywrite (photo );
}
Dr. Close ();
Myconn. Close ();

Or

Sqlconnection myconn = new sqlconnection ("Data Source = 127.0.0.1; initial catalog = testdb; user id = sa; Password = sa ");
Sqldataadapter myda = new sqldataadapter ("select personphoto from personphoto where personname = 'wangwu'", myconn );
Dataset myds = new dataset ();
Myconn. open ();
Myda. Fill (myds );
Myconn. Close ();
//
Byte [] photo = (byte []) myds. Tables [0]. Rows [0] ["personphoto"];
This. response. binarywrite (photo );

VB. NET

Dim myconn as sqlconnection = new sqlconnection ("Data Source = 127.0.0.1; initial catalog = testdb; user id = sa; Password = sa ")
Dim strcomm as string = "select personphoto from personphoto where personname = 'wangwu '"
Dim mycomm as sqlcommand = new sqlcommand (strcomm, myconn)
Myconn. Open
Dim Dr as sqldatareader = mycomm. executereader

While dr. Read
Dim photo () as byte = ctype (DR ("personphoto"), byte ())
Me. response. binarywrite (photo)

End while
Dr. Close
Myconn. Close

Or

Dim myconn as sqlconnection = new sqlconnection ("Data Source = 127.0.0.1; initial catalog = testdb; user id = sa; Password = sa ")
Dim myda as sqldataadapter = new sqldataadapter ("select personphoto from personphoto where personname = 'wangwu'", myconn)
Dim myds as dataset = new dataset
Myconn. Open
Myda. Fill (myds)
Myconn. Close
Dim photo () as byte = ctype (myds. Tables (0). Rows (0) ("personphoto"), byte ())
Me. response. binarywrite (photo)

3. Set the image control to display the binary image read from the database.

C #
---------------------------------------------
Sqlconnection myconn = new sqlconnection ("Data Source = 192.168.0.36; initial catalog = testdb; user id = sa; Password = sa ");
Sqldataadapter myda = new sqldataadapter ("select personphoto from personphoto where personname = 'wangwu'", myconn );
Dataset myds = new dataset ();
Myconn. open ();
Myda. Fill (myds );
Myconn. Close ();
//
Byte [] photo = (byte []) myds. Tables [0]. Rows [0] ["personphoto"];
// Image path
String strpath = "~ /Photo/wangwu. jpg ";
String strphotopath = server. mappath (strpath );
// Save the image file
Binarywriter BW = new binarywriter (file. Open (strphotopath, filemode. openorcreate ));
Bw. Write (photo );
Bw. Close ();
// Display the image
This. image1.imageurl = strpath;

VB. NET

Dim myconn as sqlconnection = new sqlconnection ("Data Source = 192.168.0.36; initial catalog = testdb; user id = sa; Password = sa ")
Dim myda as sqldataadapter = new sqldataadapter ("select personphoto from personphoto where personname = 'wangwu'", myconn)
Dim myds as dataset = new dataset
Myconn. Open
Myda. Fill (myds)
Myconn. Close
Dim photo () as byte = ctype (myds. Tables (0). Rows (0) ("personphoto"), byte ())
Dim strpath as string = "~ /Photo/wangwu. jpg"
Dim strphotopath as string = server. mappath (strpath)
Dim BW as binarywriter = new binarywriter (file. Open (strphotopath, filemode. openorcreate ))
Bw. Write (photo)
Bw. Close
'Show Image
Me. image1.imageurl = strpath

4. The imagefield in the gridview displays the image in URL mode.
----------------------------
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false">
<Columns>
<Asp: boundfield datafield = "personname" headertext = "name"/>
<Asp: imagefield dataimageurlfield = "personphotopath"
Headertext = "image">
</ASP: imagefield>
</Columns>
</ASP: gridview>
Bind directly to the background

5. display the Read Binary Image in the gridview
------------------------------
// Sample column
<Asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" onrowdatabound = "gridview1_rowdatabound">
<Columns>
<Asp: boundfield datafield = "personname" headertext = "name"/>
<Asp: imagefield dataimageurlfield = "personphotopath"
Headertext = "image">
</ASP: imagefield>
<Asp: templatefield headertext = "image">
<Itemtemplate>
<Asp: Image id = "image1" runat = "server"/>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>

// Bind

C #
Protected void gridview1_rowdatabound (Object sender, gridviewroweventargs E)
{
If (E. Row. rowindex <0)
Return;
// System. componentmodel. Container
String strpersonname = (string) databinder. eval (E. Row. dataitem, "personname ");
Image tmp_image = (image) E. Row. cells [2]. findcontrol ("image1 ");
If (! System. Convert. isdbnull (databinder. eval (E. Row. dataitem, "personphoto ")))
{
//
Byte [] photo = (byte []) databinder. eval (E. Row. dataitem, "personphoto ");
// Image path
String strpath = "~ /Photo/"+ strpersonname. Trim () +". jpg ";
String strphotopath = server. mappath (strpath );
// Save the image file
Binarywriter BW = new binarywriter (file. Open (strphotopath, filemode. openorcreate ));
Bw. Write (photo );
Bw. Close ();
// Display the image
Tmp_image.imageurl = strpath;
}
}
VB. NET

Protected sub gridview1_rowdatabound (byval sender as object, byval e as gridviewroweventargs)
If (E. Row. rowindex <0) then
Return
End if
'System. componentmodel. Container
Dim strpersonname as string = ctype (databinder. eval (E. Row. dataitem, "personname"), string)
Dim tmp_image as image = ctype (E. Row. cells (2). findcontrol ("image1"), image)
If not system. Convert. isdbnull (databinder. eval (E. Row. dataitem, "personphoto") then
'
Dim photo () as byte = ctype (databinder. eval (E. Row. dataitem, "personphoto"), byte ())
'G
Dim strpath as string = ("~ /Photo /"_
+ (Strpersonname. Trim + ". jpg "))
Dim strphotopath as string = server. mappath (strpath)
'Xg
Dim BW as binarywriter = new binarywriter (file. Open (strphotopath, filemode. openorcreate ))
Bw. Write (photo)
Bw. Close
'>: G
Tmp_image.imageurl = strpath
End if
End sub

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1644108

 

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.