您的位置:首页 > 数据库

详解vb.net实现图片以二进制形式上传和读取到数据库

2010-10-29 20:23 716 查看
 本篇文章旨在vb.net下实现图片以二进制形式存到数据库,并能从数据库中读取显示出来,下面附了运行后的效果和代码例子
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">
<
4000
span style="font-size:small;">         <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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息