導航:首頁 > 編程大全 > 直接向資料庫中添加圖片

直接向資料庫中添加圖片

發布時間:2023-01-24 20:11:06

『壹』 如何向資料庫中添加圖片

從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

『貳』 如何直接向資料庫中添加圖片,以便於自己網頁的調用

合理的方法是:上傳一個圖片,以xxx.jpg/xxx.gif/xxx.png這樣的文件形式儲存在一個文件夾里,然後把它對應的網址寫入資料庫即可。
如:
我用upload.asp上傳文件,設置保存路徑為/myfiles/
保存後文件名為:2009011800001.gif
我在資料庫里寫的是 rs("imgURL")="/myfiles/2009011800001.gif"
讀取的時候,輸出到網頁上是這樣:<img src="<%=rs("imgURL")%>">

千萬不要把圖片以二進制形式存到庫裡面,一是資料庫膨脹太快,二是讀取的時候慢。

『叄』 如何在資料庫中添加圖片

兩種方法:
一、存來儲圖片路源徑到資料庫,調用的時候採取如<img src="<%=rs("img")%>">的形式

二、將圖片數據以二進制流存入資料庫,就是在建表時把圖片欄位類型設置為OLE 對象

第一個方法你可以看看:http://..com/question/68793867.html

第二個方法你可以看看:http://www.knowsky.com/417.html

『肆』 SQL資料庫如何在表裡插入圖片

圖片保存來在資料庫中有兩種方式:
1.看一源下欄位的欄位類型,如果是二進制(image)的就要將圖片進行二進制轉換;
2.如果是字元串型(nvarchar)的,則直接保存圖片路徑,然後將圖片拷到那個路徑下即可.
一般圖片都是只存路徑的,存二進制占資料庫空間,會導致資料庫過大

『伍』 如何將圖片插入到資料庫中

第一步://獲取當前選擇的圖片 this.pictureBox1.Image = Image.FromStream(this.openFileDialog1.OpenFile()); //獲取當前圖片的路徑 string path = openFileDialog1.FileName.ToString(); //將制定路徑的圖片添加到FileStream類中 FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); //通過FileStream對象實例化BinaryReader對象 BinaryReader br = new BinaryReader(fs); //通過BinaryReader類對象的ReadBytes()方法將FileStream類對象轉化為二進制數組 byte[] imgBytesIn = br.ReadBytes(Convert.ToInt32(fs.Length));第二步://將圖片添加到資料庫中 string sql="insert into pic values(@pic)"; SqlParameter[] param = new SqlParameter[] { new SqlParameter("@pic", imgBytesIn) }; DBHelper.GetExecuteQuery(sql, param);第三步://將圖片從資料庫中取出 string sql="select * from pic where id=0"; SqlDataReader reader = DBHelper.GetExecuteReader(sql, null); MemoryStream mss = null;

閱讀全文

與直接向資料庫中添加圖片相關的資料

熱點內容
壓縮文件解壓打開方式 瀏覽:86
高中生查成績用哪個app 瀏覽:874
win10家庭組無法離開 瀏覽:102
微信插件文件 瀏覽:493
不讓修改的pdf文件 瀏覽:946
會聲會影模板文件格式 瀏覽:59
iphone6郵件刪除容量 瀏覽:784
暑假編程培訓怎麼學 瀏覽:88
對商家怎麼推廣app 瀏覽:92
xplane10安卓破解 瀏覽:309
下載中國知網免費入口登入工具 瀏覽:959
台達編程軟體如何下載安裝 瀏覽:758
c程序設計試題匯編譚浩強pdf 瀏覽:28
任務欄出現的文件在哪裡 瀏覽:119
房山網路大學如何報名 瀏覽:252
磊科無線路由器忘記用戶名和密碼 瀏覽:810
怎麼快速建word文件夾 瀏覽:372
怎麼把微信pdf文件發到qq郵箱 瀏覽:736
招標人在招標文件中公布的內容 瀏覽:793
資料庫技術有哪些工程應用 瀏覽:471

友情鏈接