您的位置:首页 > 数据库

图片二进制数据库的存取和读取...

2008-11-24 08:54 218 查看
图片二进制数据库的存取和读取

DataBase Table Desing:



-Function:

1. 导入图像文件,以二进制形式存入数据库表。

2. 从数据库表读取二进制信息,返原显示在页面的datagrid 中

---Web.Config---

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<!-- application specific settings -->

<appSettings>

<add key="uid" value="sa" />

<add key="pwd" value="" />

<add key="server" value="10.33.220.87" />

<add key="database" value="test" />

</appSettings>

<system.web>

---Code Section WebForm2.aspx---

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="EmployeeInfo.WebForm2"%>

<HTML>

<HEAD>

<title>WebForm2</title>

<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">

<meta content="JavaScript" name="vs_defaultClientScript">

<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

</HEAD>

<body MS_POSITIONING="GridLayout">

<form id="Form1" method="post" runat="server">

<INPUT id="File1" style="Z-INDEX: 101; LEFT: 128px; POSITION: absolute; TOP: 72px" type="file"

runat="server">

<asp:datagrid id="DataGrid1" style="Z-INDEX: 106; LEFT: 24px; POSITION: absolute; TOP: 208px"

runat="server" Width="464px" Height="136px" AutoGenerateColumns="False">

<Columns>

<asp:BoundColumn DataField="BadgeNO" HeaderText="Badge No"></asp:BoundColumn>

<asp:TemplateColumn HeaderText="Photo">

<ItemTemplate>

<asp:Image ID="Photo" Runat="server"></asp:Image>

</ItemTemplate>

<HeaderStyle Width="160px" />

</asp:TemplateColumn>

</Columns>

</asp:datagrid><asp:label id="Label1" style="Z-INDEX: 102; LEFT: 24px; POSITION: absolute; TOP: 184px" runat="server"

Width="152px" Height="16px" BackColor="Gray" Font-Bold="True">Employee Report</asp:label><asp:button id="Button_Insert" style="Z-INDEX: 103; LEFT: 384px; POSITION: absolute; TOP: 72px"

runat="server" Text="Insert" Width="56px"></asp:button>

<asp:TextBox id="Tbx_BadgeNo" style="Z-INDEX: 104; LEFT: 128px; POSITION: absolute; TOP: 40px"

runat="server"></asp:TextBox>

<asp:Button id="Button_Report" style="Z-INDEX: 105; LEFT: 384px; POSITION: absolute; TOP: 112px"

runat="server" Text="Report"></asp:Button></form>

</body>

</HTML>

---Code Section WebFor2.vb---

Imports System

Imports System.Drawing

Imports System.IO

Imports System.Data.SqlClient

Public Class WebForm2

Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile

Protected WithEvents Tbx_BadgeNo As System.Web.UI.WebControls.TextBox

Protected WithEvents Button_Insert As System.Web.UI.WebControls.Button

Protected WithEvents Button_Report As System.Web.UI.WebControls.Button

Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web Form Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private connString As String

Private Sub InitalDB()

Dim uid = ConfigurationSettings.AppSettings("uid")

Dim pwd = ConfigurationSettings.AppSettings("pwd")

Dim server = ConfigurationSettings.AppSettings("server")

Dim database = ConfigurationSettings.AppSettings("database")

connString = "server=" & server & ";uid=" & uid & ";pwd=" & pwd & ";database=" & database

End Sub

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

InitalDB()

End Sub

Private Sub Button_Report_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Report.Click

ShowPhoto()

End Sub

Private Sub ShowPhoto()

Dim dt As New DataTable

Dim myConn As SqlConnection = New SqlConnection(connString)

Dim sql As String = " SELECT * FROM tb_1 "

myConn.Open()

Dim adp As New SqlDataAdapter(sql, myConn)

adp.Fill(dt)

For lint_index As Integer = 0 To dt.Rows.Count - 1

Dim photo() As Byte = CType(dt.Rows(lint_index).Item("Photo"), Byte())

' Me.Response.BinaryWrite(photo)

Dim lstg_badgeno As String

lstg_badgeno = dt.Rows(lint_index).Item("BadgeNo")

Dim strPath As String = "~/photo/" + lstg_badgeno + ".JPG"

Dim strPhotoPath As String = Server.MapPath(strPath)

Dim bw As BinaryWriter = New BinaryWriter(File.Open(strPhotoPath, FileMode.OpenOrCreate))

bw.Write(photo)

bw.Close()

Next

myConn.Close()

Me.DataGrid1.DataSource = dt

Me.DataGrid1.DataBind()

UpdatePhoto()

End Sub

Private Sub Button_Insert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Insert.Click

Try

Dim strPath As String = Me.File1.PostedFile.FileName

Dim BadgeNo As String = Me.Tbx_BadgeNo.Text

'Dim strPhotoPath As String = Server.MapPath(strPath)

'读取图片

' Dim fs As FileStream = New System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read)

Dim fs As FileStream = New System.IO.FileStream(strPath, 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()

Dim myConn As SqlConnection = New SqlConnection(connString)

Dim strComm As String = " INSERT INTO tb_1(BadgeNo,Photo) "

strComm = (strComm + (" VALUES('" + BadgeNo + "', @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()

Catch ex As Exception

Response.Write(ex.ToString)

End Try

ShowPhoto()

End Sub

'---Bind Photo---

Private Sub UpdatePhoto()

For Each lobj_dgi As DataGridItem In Me.DataGrid1.Items

Dim tmp_Image As System.Web.UI.WebControls.Image = CType(lobj_dgi.Cells(1).FindControl("Photo"), System.Web.UI.WebControls.Image)

Dim lstg_badgeno As String = lobj_dgi.Cells(0).Text

tmp_Image.ImageUrl = "~/photo/" + lstg_badgeno + ".JPG"

Next

End Sub

End Class
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: