With the image of the binary database storage and display

Source: Internet
Author: User

1. Store pictures in binary database
2. Read the binary picture on the page display
3. Set the image control to display a binary picture read from the database
4.GridView imagefield display picture in URL mode
5.GridView display of Read binary pictures
====================

1. Store pictures in binary database

C#
--------------------------
Save pictures to Database
protected void Button1_Click (object sender, EventArgs e)
{
Picture path
String strpath = "~/photo/03.jpg";
String strphotopath = Server.MapPath (strpath);
Reading pictures
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 ();
Deposit
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 pictures to Database

Protected Sub button1_click (ByVal sender as Object, ByVal e as EventArgs)
' Picture path
Dim strpath as String = "~/photo/03.jpg"
Dim Strphotopath as String = Server.MapPath (strpath)
' Read the picture
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 (fs. CType. Length,integer))
Br. Close
Fs. Close
' Deposit
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" (' Wangwu ', ' "_
+ (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 the binary picture on the page display

C#
--------------------------
Reading pictures
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 a binary picture 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"];
Picture path
String strpath = "~/photo/wangwu. JPG ";
String strphotopath = Server.MapPath (strpath);
Save picture file
BinaryWriter bw = new BinaryWriter (File.Open (strphotopath,filemode.openorcreate));
Bw. Write (photo);
Bw. Close ();
Show pictures
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= ' W ") Angwu ', 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 (strph Otopath, FileMode.OpenOrCreate))
bw. Write (photo)
bw. Close
Show picture
Me.Image1.ImageUrl = strpath

4.GridView imagefield display picture in URL mode
----------------------------
<asp:gridview id= "GridView1" runat= "Server" autogeneratecolumns= "False" >
<Columns>
<asp:boundfield datafield= "PersonName" headertext= "name"/>
<asp:imagefield dataimageurlfield= "Personphotopath"
headertext= "Pictures" >
</asp:ImageField>
</Columns>
</asp:GridView>
Background direct binding can be

5.GridView display of Read binary pictures
------------------------------
Boilerplate column
<asp:gridview id= "GridView1" runat= "Server" autogeneratecolumns= "False" onrowdatabound= "GridView1_RowDataBound" >
<Columns>
<asp:boundfield datafield= "PersonName" headertext= "name"/>
<asp:imagefield dataimageurlfield= "Personphotopath"
headertext= "Pictures" >
</asp:ImageField>
<asp:templatefield headertext= "Pictures" >
<ItemTemplate>
<asp:image id= "Image1" runat= "Server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Binding

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");
Picture path
String strpath = "~/photo/" + strpersonname.trim () + ". JPG ";
String strphotopath = Server.MapPath (strpath);
Save picture file
BinaryWriter bw = new BinaryWriter (File.Open (Strphotopath, FileMode.OpenOrCreate));
Bw. Write (photo);
Bw. Close ();
Show pictures
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

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.