當前位置:首頁 » 操作系統 » 向資料庫添加圖片

向資料庫添加圖片

發布時間: 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,單擊打開它。

熱點內容
磁碟禁止訪問 發布:2024-04-25 22:53:48 瀏覽:286
多線程ftp上傳 發布:2024-04-25 22:41:36 瀏覽:114
phpqrcode 發布:2024-04-25 22:41:36 瀏覽:32
桂平上網密碼是多少 發布:2024-04-25 22:32:10 瀏覽:574
open函數c語言 發布:2024-04-25 21:47:42 瀏覽:406
簡訊刪除後怎麼找伺服器 發布:2024-04-25 21:15:06 瀏覽:388
查ip地址伺服器數量 發布:2024-04-25 20:49:48 瀏覽:620
安卓手機單核性能為什麼不高 發布:2024-04-25 20:48:07 瀏覽:56
群暉php 發布:2024-04-25 20:00:35 瀏覽:884
怎麼查看我的wifi密碼 發布:2024-04-25 18:54:43 瀏覽:757