如何向数据库中添加图片

请高人将具体一点,我是个高级菜鸟... 请高人将具体一点,我是个高级菜鸟 展开
 我来答
FM网络
2009-01-18 · TA获得超过825个赞
知道小有建树答主
回答量:1130
采纳率:0%
帮助的人:967万
展开全部
从SQL Server数据库中读取并显示图片

ASPX:

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<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>

<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>

VB.NET:

Imports System
Imports System.Drawing
Imports System.IO
Imports System.Data.SqlClient
Imports System.Data.SqlDbType

Partial 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

'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 As String = "sa"
Dim pwd As String = "fm"
Dim server As String = "192.168.1.1"
Dim database As String = "FM"
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 Data.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 = "~/photoWeb/" + 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", System.Data.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 = "~/photoWeb/" + lstg_badgeno + ".JPG"
Next
End Sub
End Class

参考资料: http://hi.baidu.com/fm%CD%F8%C2%E7/blog/item/e4fd6ff4f370f622bd31090f.html

平静且妥善的小鱼丸998
2009-01-17 · TA获得超过174个赞
知道小有建树答主
回答量:276
采纳率:0%
帮助的人:199万
展开全部
现在的技术都不用数据库存图片了
可以直接把图片的路径存在数据库里,然后把图片通过上传程序上指定的位置
然后通过调用路径来调出图片
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
25zhangkun
2009-01-17 · TA获得超过104个赞
知道答主
回答量:183
采纳率:0%
帮助的人:0
展开全部
把图片用二进制写进去不就行了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式