导航:首页 > 编程大全 > 直接向数据库中添加图片

直接向数据库中添加图片

发布时间: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;

阅读全文

与直接向数据库中添加图片相关的资料

热点内容
对商家怎么推广app 浏览:92
xplane10安卓破解 浏览:309
下载中国知网免费入口登入工具 浏览:959
台达编程软件如何下载安装 浏览:758
c程序设计试题汇编谭浩强pdf 浏览:28
任务栏出现的文件在哪里 浏览:119
房山网络大学如何报名 浏览:252
磊科无线路由器忘记用户名和密码 浏览:810
怎么快速建word文件夹 浏览:372
怎么把微信pdf文件发到qq邮箱 浏览:736
招标人在招标文件中公布的内容 浏览:793
数据库技术有哪些工程应用 浏览:471
如何将cad中的文字复制到word中 浏览:772
string转jsonjs 浏览:810
如何反制清洁网络计划 浏览:500
数据库数据库实例是什么意思 浏览:756
6splus南京东路苹果专卖店报价 浏览:688
vm获取配置文件中变量 浏览:639
三星note2哪个版本好 浏览:405
c程序求5 浏览:736

友情链接