当前位置:首页 » 操作系统 » 向数据库添加图片

向数据库添加图片

发布时间: 2022-08-07 16:05:08

⑴ 如何向数据库中添加图片

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

⑵ 如何在数据库中添加图片

我使用过,我一般是这么处理的,如下(我用的vb,你可以转变成access里的代码):

图片是可以放到数据库里面的,以ACCESS为例子,要使用OLE数据类型,
然后把图片转化为长二进制数据存入,读取的时候可以生成系统隐藏的图
片然后在图片控件中显示

Dim cc() As Byte
Dim i
Dim t
Private Sub Command1_Click()
CommonDialog1.Filter = "JPG图片(*.BMP)|*.BMP"
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
Picture1.Picture = LoadPicture(CommonDialog1.FileName)
strname = CommonDialog1.FileName
Open strname For Binary As #1
ReDim cc(LOF(1) - 1)
t = cc(LOF(1) - 1) '在读取二进制时,如果是用文件操作那么涉及到到底从文件中取多少字节的问题,所以字节数组要指定大小
Get #1, , cc
Close
Else
MsgBox "没有选中图片"
End If
End Sub
Private Sub Command2_Click()
Adodc1.Refresh
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields("id") = i
Adodc1.Recordset.Fields("qq") = cc
i = i + 1
Adodc1.Recordset.Update
Adodc1.Refresh
End Sub
Private Sub Command3_Click()
Adodc1.Refresh
Dim P() As Byte
Adodc1.RecordSource = "select qq from 表一 where id='" & Text1.Text & "'"
P = Adodc1.Recordset.Fields("qq") '当把一组二进制数据赋值给一个二进制数组时就不用指定数组的大小了,因为是全部赋值
Open App.Path & "\oo.bmp" For Binary As #1
Put #1, , P
Close
End Sub
Private Sub Command4_Click()
Open App.Path & "\oo.bmp" For Binary As #1
Put #1, , cc
Close
End Sub
Private Sub Form_Load()
i = 1
End Sub

⑶ 怎样在数据库中插入图片

把图片在计算机中的地址保存到数据库中,

取的时候就取地址就可以了

如果按你们老师说的 那你那个数据库会太大的 到时候 你这个库备份起来就麻烦了,万一移动一下 那工程量就大了

路径不就是个字符串嘛,在数据库里不就是varchar吗,如果你会插入/更新数据库里varchar类型的数据,这自然就应该会了嘛

不好意思哦 你的这个不能存路径的问题我实在是没碰到过 你还是找下别的高手吧

⑷ 怎样往SQL数据库中插入图片,最好举例说明

首先存储图片主要是要保存到一个表内的字段里。要确定保存的字段类型为二进制数组等图片可用的类型,
然后一般的sql工具都能把图片变成二进制序列。到时候直接存入的时候存成
2进制
数列就可以。
等取出的时候用二进制流取出然后做成跟文件,然后拼接上原来存入文件的
扩展名
就是你刚才存入的文件。

⑸ 图片如何存入数据库

1、新建一个数据库,数据库名为Image,表名为image。并为表添加ID,tupian两个列。

⑹ 关于如何向数据库(Sql Sever)添加图片的问题

往数据库存储图片一般有两种情况

  1. 存储路径

  2. 存储图片,这个需要转换成二进制流

    OpenFileDialog sf = new OpenFileDialog();
    sf.Filter = "(*.jpg)|*.jpg";
    sf.ShowDialog();
    Image img = Image.FromFile(sf.FileName);
    然后把img存入数据库就好了 。这种方式性能不好延迟有点大。

一般都会采用第一种方式。

⑺ 怎么在sql数据库中的表里边添加图片

1、打开开始菜单栏,在菜单栏上找到我们已经安装的SQL server 2008,单击打开它。

热点内容
sql转换成数据类型int时失败 发布:2024-05-05 06:29:21 浏览:826
苹果手机视频怎么加密 发布:2024-05-05 06:22:08 浏览:918
java反编译工具使用方法 发布:2024-05-05 06:00:38 浏览:217
恋人源码 发布:2024-05-05 05:53:33 浏览:166
安卓平板用什么助手好 发布:2024-05-05 05:51:09 浏览:775
java语义分析 发布:2024-05-05 05:32:39 浏览:754
我的世界服务器房型 发布:2024-05-05 05:31:16 浏览:702
pythonwhere 发布:2024-05-05 05:30:22 浏览:441
免费加密隐身侠 发布:2024-05-05 05:07:54 浏览:613
我的世界模组服务器推荐手机版 发布:2024-05-05 05:02:49 浏览:818