VB中上传下载文件到SQL数据库
编写人:左丘文
2015-4-11
近期在修改一个VB编写的系统时,想给画面增加一个上传文件到数据库,并可以下载查看的功能,今天在这里,我想与大家一起分享代码,在此做个小结,以供参考。有兴趣的同学,可以一同探讨与学习一下,否则就略过吧。
1、 首先要保存文件到数据库,我们需要使用流对象保存,所以第一时间先在数据库中增加
一个image的字段(注意:Access中的photo字段类型为OLE对象.SqlServer中的photo字段类型为Image),用于存储文件。
2、 下面再在画面增加用于上传及下载的CommandButton及一个commondialog:
1) 数据库连接处理
1 Private Sub Form_Load()
2 '数据库连接字符串
3 Connstring="Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _
4 ";Data Source=d:\csdn_vb\database\保存图片\access图片\img.mdb"
5
6 ‘下面的语句是连接sqlserver数据库的.
7 ‘Connstring="Provider=SQLOLEDB.1;Persist Security Info=True;" & _
8 ‘"User ID=sa;Password=;Initial Catalog=test;Data Source=yang"
9 Set Conn = New ADODB.Connection
Conn.Open Connstring
End Sub
2) 上传文件处理
1 Private Sub cmdUpload_Click()
2 ** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
3 ‘.5版本以下不支持Stream对象
4
5 On Error GoTo handleErr
6 Dim rs As ADODB.Recordset
7 Dim mstream As ADODB.Stream
8
9 '保存文件到数据库中
Set rs = New ADODB.Recordset
With rs
.ActiveConnection = Conn
.LockType = adLockOptimistic
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.Open "SELECT * from SR WHERE SRNUM='" & txtSRNUM.Text & "'"
End With
CommonDialog1.Filter = "Pictures (*.PDF;*.pdf)|*.PDF;*.pdf"
CommonDialog1.ShowOpen
If CommonDialog1.filename = "" Then Exit Sub
If (rs.RecordCount = ) Then
'读取文件到内容
Set mstream = New ADODB.Stream
With mstream
.Type = adTypeBinary '二进制模式
.Open
.LoadFromFile CommonDialog1.filename
End With
rs.Fields("FileName").Value = CommonDialog1.FileTitle
rs.Fields("FileUploadTime").Value = Format(Now, "YYYY-MM-DD hh:mm")
rs.Fields("FileNameContent") = mstream.Read
rs.update
'完成后关闭对象
mstream.Close
End If
rs.Close
Set rs = Nothing
txtFileName.Text = CommonDialog1.FileTitle
Exit Sub
handleErr:
MsgBox ERR.Description
End Sub
2 ** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本
3 ‘.5版本以下不支持Stream对象
4
5 On Error GoTo handleErr
6 Dim rs As ADODB.Recordset
7 Dim mstream As ADODB.Stream
8
9 '保存文件到数据库中
Set rs = New ADODB.Recordset
With rs
.ActiveConnection = Conn
.LockType = adLockOptimistic
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.Open "SELECT * from SR WHERE SRNUM='" & txtSRNUM.Text & "'"
End With
CommonDialog1.Filter = "Pictures (*.PDF;*.pdf)|*.PDF;*.pdf"
CommonDialog1.ShowOpen
If CommonDialog1.filename = "" Then Exit Sub
If (rs.RecordCount = ) Then
'读取文件到内容
Set mstream = New ADODB.Stream
With mstream
.Type = adTypeBinary '二进制模式
.Open
.LoadFromFile CommonDialog1.filename
End With
rs.Fields("FileName").Value = CommonDialog1.FileTitle
rs.Fields("FileUploadTime").Value = Format(Now, "YYYY-MM-DD hh:mm")
rs.Fields("FileNameContent") = mstream.Read
rs.update
'完成后关闭对象
mstream.Close
End If
rs.Close
Set rs = Nothing
txtFileName.Text = CommonDialog1.FileTitle
Exit Sub
handleErr:
MsgBox ERR.Description
End Sub
3) 下载文件处理
1 Private Sub cmdDownload_Click()
2 On Error GoTo handleErr
3 Dim rs As ADODB.Recordset
4 Dim mstream As ADODB.Stream
5
6 Set rs = New ADODB.Recordset
7 With rs
8 .ActiveConnection = ConGamma
9 .LockType = adLockOptimistic
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.Open "SELECT * from SR WHERE SRNUM='" & txtSRNUM.Text & "'"
End With
If (rs.RecordCount = ) Then
If (rs("FileNameContent").ActualSize > ) Then ‘判断是否为空
'保存到文件
Set mstream = New ADODB.Stream
With mstream
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write rs("FileNameContent")
.SaveToFile "C:\8D.PDF" ‘‘这里注意了,如果当前目录下存在8D.PDF,会报一个文件写入失败的错误.
End With
'关闭对象
rs.Close
mstream.Close
End If
End If
Exit Sub
handleErr:
MsgBox ERR.Description
End Sub
2 On Error GoTo handleErr
3 Dim rs As ADODB.Recordset
4 Dim mstream As ADODB.Stream
5
6 Set rs = New ADODB.Recordset
7 With rs
8 .ActiveConnection = ConGamma
9 .LockType = adLockOptimistic
.CursorLocation = adUseClient
.CursorType = adOpenKeyset
.Open "SELECT * from SR WHERE SRNUM='" & txtSRNUM.Text & "'"
End With
If (rs.RecordCount = ) Then
If (rs("FileNameContent").ActualSize > ) Then ‘判断是否为空
'保存到文件
Set mstream = New ADODB.Stream
With mstream
.Mode = adModeReadWrite
.Type = adTypeBinary
.Open
.Write rs("FileNameContent")
.SaveToFile "C:\8D.PDF" ‘‘这里注意了,如果当前目录下存在8D.PDF,会报一个文件写入失败的错误.
End With
'关闭对象
rs.Close
mstream.Close
End If
End If
Exit Sub
handleErr:
MsgBox ERR.Description
End Sub
具体就需要各位好好的去自已去调试了。
3、有关更多的技术分享,大家可以加入我们的技术群。
欢迎加入技术分享群:238916811