vb.net 中如何将图片保存到SQL数据库
如题,把图片保存到SQL数据库当中,有人推荐使用“保存路径”的方法。但是具体怎么做???如果想要直接保存到数据库当中,又该怎么做???
可以的话给出具体的步骤和代码,以及注释
新人提问,答案越详细越好。
追问:具体怎么做??
如题,把图片保存到SQL数据库当中,有人推荐使用“保存路径”的方法。但是具体怎么做???如果想要直接保存到数据库当中,又该怎么做???
可以的话给出具体的步骤和代码,以及注释
新人提问,答案越详细越好。
追问:具体怎么做??
答案:(注意在看的时候,也没有必要全部看,首先看骨架,然后在看图片怎么保存到数据库中的就行了,另外,这里涉及到了fileSream流,主要是用来读取数据,与此相关的还有网络方面的NetworkStream,呵呵,还有很多)
微软的SQL SERVER数据库的Image、text等字段都属于二进制的大对象。这些对象的存取和其他轻型对象略有不同。比如,我们打开一个数据表的时候,普通类型的字段都可以看见,而Image类型的字段却不行,只能通过编程的方法来读取。这篇资料就是介绍怎样用vb.NET来向sql server数据库中存放图片,怎样从数据库中取出图片浏览
这个例程用到了 sql server自带的northwind数据库,其中employees(职员)数据表有一个字段“photo”,是用来存放照片的,里面有9条记录,我费了半天力气,想去看看那9个人图片,就是看不见。现在猜测,这些人的photo根本就是空的。所以,我决定添加一些新的记录。
在这个例程里,我实现了一个简单的WinForm程序,这个程序通过点击“Open”按钮选择一个图片文件(bmp或jpg),并显示在图形控件PictureBox中。通过“Save”按钮存入数据库。点击“View”CheckBox可以切换到浏览状态,观看存入数据库的图片。
微软.NET Framework的System.IO命名空间下给我们提供了一个FileStream文件流类。我们可以使用这个文件流对二进制大对象轻松进行读写。由于对二进制大对象使用的流操作,所以对于任何文件都具有通用性。读写文本文件也可以这么做。
步骤:1、 先来看一下northwind数据库中employees表结构
2、 知道.net连接sql server的方法嘛,不知道的话,看看我写的“ado.net中的connection对象”吧。读懂下面这几句没问题吧:Dim conn as new sqlconnection(“server=localhost;database=northwind;Integrated Security=true;”
Dim sqlcomm As New SqlCommand
sqlcomm.Connection = conn
sqlcomm.CommandText = "INSERT INTO employees (lastname,firstname,photo) VALUES (@lastname,@firstname,@photo)"
Dim prm1 As New SqlParameter("@lastname", txtLN.Text)
Dim prm2 As New SqlParameter("@firstname", txtFN.Text)
Dim prm3 As New SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length), _ ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, Data)
sqlcomm.Parameters.Add(prm1)
sqlcomm.Parameters.Add(prm2)
sqlcomm.Parameters.Add(prm3)
3、新建一个windows应用程序窗体
4、 声明类级的变量。找到设计期产生的代码部分,把下面的语句加到变量声明后面。什么?不知道变量声明在哪儿?不会吧!Private fs As FileStream
Private ds As DataSet
Private conn As New SqlConnection("server=localhost;database=northwind;integrated security=true;")
Private currentpos As Integer = 9
5、 开始写代码了。首先是form_load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnOpen.Enabled = True
btnSave.Enabled = True
btnBack.Enabled = False
btnForward.Enabled = False
End Sub
6、 open按钮的点击事件代码:
Private Sub openbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
Dim Opendlg As New OpenFileDialog
Opendlg.Title = "Select a picture file"
Opendlg.Filter = "(*.bmp)|*bmp|(*.jpg)|*.jpg"
Opendlg.ShowDialog()
lblPath.Text = Opendlg.FileName.ToString()
fs = New FileStream(Opendlg.FileName.ToString(), FileMode.Open, FileAccess.Read)
PictureBox1.Image = Image.FromFile(Opendlg.FileName.ToString())
End Sub
7、 save按钮的点击事件代码
Private Sub savebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim sqlcomm As New SqlCommand
sqlcomm.CommandText = "INSERT INTO employees (lastname,firstname,photo) VALUES (@lastname,@firstname,@photo)"
sqlcomm.Connection = conn
Dim Data(fs.Length) As Byte
fs.Read(Data, 0, Int(fs.Length))
Dim prm1 As New SqlParameter("@lastname", txtLN.Text)
Dim prm2 As New SqlParameter("@firstname", txtFN.Text)
Dim prm3 As New SqlParameter("@photo", SqlDbType.VarBinary, Int(fs.Length), ParameterDirection.Input, False, 0, 0, "", DataRowVersion.Current, Data)
sqlcomm.Parameters.Add(prm1)
sqlcomm.Parameters.Add(prm2)
sqlcomm.Parameters.Add(prm3)
Try
conn.Open()
sqlcomm.ExecuteNonQuery() '执行插入语句
conn.Close()
fs.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
8、 选择view状态的事件代码
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
btnOpen.Enabled = False
btnSave.Enabled = False
btnBack.Enabled = True
btnForward.Enabled = True
currentpos = 9
Dim sqlcomm As New SqlCommand
sqlcomm.CommandText = "SELECT employeeId, photo FROM employees ORDER BY employeeId"
sqlcomm.Connection = conn
Dim da As New SqlDataAdapter(sqlcomm)
Try
conn.Open()
ds = New DataSet
da.Fill(ds, "employees")
conn.Close()
Catch sqlEx As SqlException
MsgBox(sqlEx.Message)
End Try
Dim data() As Byte = ds.Tables("employees").Rows(9)("photo")
Dim stmphoto As New MemoryStream(data)
PictureBox1.Image = Image.FromStream(stmphoto)
Else
btnOpen.Enabled = True
btnSave.Enabled = True
btnBack.Enabled = False
btnForward.Enabled = False
End If
End Sub
9、 “>>”按钮点击事件代码
Private Sub forward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForward.Click
If currentpos = ds.Tables("employees").Rows.Count - 1 Then
Return
Else
currentpos += 1
Dim data() As Byte
data = ds.Tables("employees").Rows(currentpos)("photo")
Dim stmphoto As New MemoryStream(data)
PictureBox1.Image = Image.FromStream(stmphoto)
End If
End Sub
10、 “<<”按钮点击事件代码
Private Sub back_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
If currentpos = 9 Then
Return
Else
currentpos -= 1
End If
Dim Data() As Byte
Data = ds.Tables("employees").Rows(currentpos)("photo")
Dim stmPhoto As New MemoryStream(Data)
PictureBox1.Image = Image.FromStream(stmPhoto)
End Sub
(我是摘录的 http://blog.chinaunix.net/u1/36875/showart_283820.html)
都是只保存路径的,把图片放到一个专用的文件夹下,读取的时候,从路径得图片
#Region "二进制文件的存储函数"
Public Function BinaryToFile(ByRef TableRowColItem As Object, ByVal FileName As String) As Boolean
Dim data As Byte() = TableRowColItem
Dim myfilestream As New System.IO.FileStream(FileName, IO.FileMode.Create)
myfilestream.Write(data, 0, data.Length)
myfilestream.Close()
Return True
End FunctionPublic Function BinaryToImage(ByRef TableRowColItem As Object, ByRef image As Image) As Boolean
Dim data As Byte() = TableRowColItem
Dim imgStream As New System.IO.MemoryStream '(data)
imgStream.Write(data, 0, data.Length)
image = System.Drawing.Image.FromStream(imgStream)
imgStream.Close()
imgStream.Dispose()Return True
End Function
Public Function BinaryFromFile(ByVal FileName As String, ByRef TableRowColItem As Object) As BooleanUsing myfilestream As New FileStream(FileName, FileMode.Open, FileAccess.Read)
Dim data() As Byte
ReDim data(myfilestream.Length - 1)
myfilestream.Read(data, 0, myfilestream.Length)
myfilestream.Close()
TableRowColItem = dataEnd Using
Return True
End FunctionPublic Function BinaryFromImage(ByRef Image As Image, ByRef TableRowColItem As Object) As Boolean
Dim imgStream As New MemoryStream
Dim b As New Bitmap(Image)b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim data As Byte() = imgStream.G