存路径的方法简单可行。下面是存图片到数据库中。 Access 中是 OLE 对象的字段 Private Sub DBOpen() 'open the database with ADO MYcon.Open "PROVIDER=MICROSOFT.JET.OLEDB.4.0; DATA SOURCE=" & App.Path + "/DBpic.MDB" MYrs.Open "PICTABLE", MYcon, 1, 3 End Sub 'Close the open database Private Sub DBClose() MYrs.Close MYcon.Close Set MYrs = Nothing Set MYcon = Nothing End Sub Private Sub SaveInto(ByVal strPath As String) Dim lngFileLength As Long 'the length of the file Dim lngBlockCount As Long 'the number of total whole block Dim lngLastBlock As Integer 'the length of the last block Dim lngBlockIndex As Long 'the index of each block Dim ByteGet() As Byte '用于传送数据的二进制数组 Dim FileNum As Integer 'return the file number which the next file will use Dim strFilepath As String strFilepath = strPath FileNum = FreeFile() Open strFilepath For Binary Access Read As #FileNum lngFileLength = LOF(FileNum) '返回一个 Long,表示用 Open 语句打开的文件的大小,该大小以字节为单位。 lngBlockCount = lngFileLength \ lngBlockSize lngLastBlock = lngFileLength Mod lngBlockSize MYrs.AddNew MYrs.Fields("size") = lngFileLength MYrs.Fields("date") = Date MYrs.Fields("name") = Trim(Text1) ReDim ByteGet(lngBlockSize) For lngBlockIndex = 1 To lngBlockCount Get #FileNum, , ByteGet() MYrs.Fields("pic").AppendChunk ByteGet() Next If lngLastBlock > 0 Then ReDim ByteGet(lngLastBlock) Get #FileNum, , ByteGet() MYrs.Fields("pic").AppendChunk ByteGet() End If MYrs.Update Close #FileNum End Sub Private Sub ShowImg(ByVal RecordPoint As Long) On Error Resume Next Dim temp_path As String Dim temp_file As String Dim length As Long Dim lngFileLength As Long 'the length of the file Dim lngBlockCount As Long 'the number of total whole block Dim lngLastBlock As Integer 'the length of the last block Dim lngBlockIndex As Long 'the index of each block Dim ByteGet() As Byte '用于传送数据的二进制数组 Dim FileNum As Integer 'return the file number which the next file will use Dim strFileName As String temp_path = Space$(MAX_PATH) length = GetTempPath(MAX_PATH, temp_path) temp_path = Left$(temp_path, length) temp_file = Space$(MAX_PATH) GetTempFileName temp_path, "per", 0, temp_file strFileName = Left$(temp_file, InStr(temp_file, Chr$(0)) - 1) MYrs.MoveFirst MYrs.Move RecordPoint Label1 = MYrs.AbsolutePosition frmMain.Caption = MYrs.Fields("name") + Str(i) FileNum = FreeFile() Open strFileName For Binary As #FileNum lngFileLength = MYrs.Fields("size") lngBlockCount = lngFileLength \ lngBlockSize lngLastBlock = lngFileLength Mod lngBlockSize For lngBlockIndex = 1 To lngBlockCount ByteGet() = MYrs.Fields("pic").GetChunk(lngBlockSize) Put #FileNum, , ByteGet() Next If lngLastBlock > 0 Then ReDim ByteGet(lngLastBlock) ByteGet() = MYrs.Fields("pic").GetChunk(lngBlockSize) Put #FileNum, , ByteGet() End If Picture1.Picture = LoadPicture(strFileName) Close #FileNum Kill strFileName Err.Clear End Sub Private Sub AimFilePath(ByVal strPath As String) Dim PathVal As String PathVal = Dir(strPath) If PathVal = Null Then MsgBox "null" Do While PathVal <> "" SaveInto (strPath + PathVal) PathVal = Dir Loop End Sub
建议使用路径! 如果真要写入数据库,字段使用“ole对象“
Private Sub add_Click(Cancel As Boolean) On Error GoTo myerror If quanxianjiancha("增加记录") = True Then Dim xx As String Dim yy As String Dim rst As ADODB.Recordset xx = "select 照片 from 照片库 where 序号=" & Val(Trim(ListView1.SelectedItem.ListSubItems(1).Text)) Set rst = chaxun(xx, yy) If rst.RecordCount > 0 Then MsgBox "有照片,加什么?", , "警告": Exit Sub Dim bit() As Byte Dim varPath As String '图片的路径 CommonDialog1.Filter = "Pictures (*.bmp;*.jpg)|*.bmp;*.jpg" CommonDialog1.ShowOpen varPath = CommonDialog1.FileName If varPath = "" Then Exit Sub xx = "select * from 照片库" Set rst = chaxun(xx, yy)
Open varPath For Binary As #1 ReDim bit(LOF(1)) As Byte Get 1, 1, bit Close 1 ' 然后将字节数组的内容写入数据库即可 rst.AddNew rst.Fields("照片").AppendChunk bit rst.Fields("序号") = Val(Trim(ListView1.SelectedItem.ListSubItems(1).Text)) rst.Update Image1.Picture = LoadPicture(varPath) Slider1.Visible = True asPopup5.Visible = True asPopup2.Visible = True asPopup3.Visible = True asPopup4.Visible = False ListView1.SelectedItem.ForeColor = vbBlue Call WriteToIni(App.Path & "\set.dat", "有照片否", "序号" & Val(Trim(ListView1.SelectedItem.ListSubItems(1).Text)), 1) zpshuliang = zpshuliang + 1 Label6.Caption = "照片总数: " & zpshuliang & " 张" Set rst = NothingElse MsgBox "无此权限", , "提示" End If Set rst = Nothing myerror: Exit Sub End SubPrivate Sub huan_Click(Cancel As Boolean) On Error GoTo myerror If quanxianjiancha("修改记录") = True Then Dim xx As String Dim yy As String Dim nrc As ADODB.Recordset Dim bit() As Byte Dim varPath As String '图片的路径 CommonDialog1.Filter = "Pictures (*.bmp;*.jpg)|*.bmp;*.jpg" CommonDialog1.ShowOpen varPath = CommonDialog1.FileName If varPath = "" Then Exit Sub xx = "select * from 照片库 where 序号=" & Val(Trim(ListView1.SelectedItem.ListSubItems(1).Text)) Set nrc = chaxun(xx, yy) Open varPath For Binary As #1 ReDim bit(LOF(1)) As Byte Get 1, 1, bit Close 1 nrc.Fields("照片").AppendChunk bit nrc.Update Image1.Picture = LoadPicture(varPath) Image1.RefreshElse MsgBox "无此权限", , "提示" End If Set nrc = Nothing Exit Submyerror: MsgBox "更改不成功", , "提示" Exit Sub End SubPrivate Sub del_Click(Cancel As Boolean) If quanxianjiancha("删除记录") = True Then Dim xx As String Dim yy As String Dim rst As ADODB.Recordset xx = "delete from 照片库 where 序号=" & Val(Trim(ListView1.SelectedItem.ListSubItems(1).Text)) Set rst = chaxun(xx, yy) MsgBox yy Image1.Picture = Nothing ListView1.SelectedItem.ForeColor = vbRed Call WriteToIni(App.Path & "\set.dat", "有照片否", "序号" & Val(Trim(ListView1.SelectedItem.ListSubItems(1).Text)), 0) zpshuliang = zpshuliang - 1 Label6.Caption = "照片总数: " & zpshuliang & " 张" Else MsgBox "无此权限", , "提示" End If Set rst = Nothing End Sub Public Function chaxun(ByVal sql As String, msgstring As String) As ADODB.Recordset On Error GoTo ExecuteSQL_Error
Dim cN As ADODB.Connection Dim rst As ADODB.Recordset Dim a() As String Set cN = New ADODB.Connection
Dim str As String str = App.Path If Right(str, 1) <> "\" Then str = str + "\" End If cN.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\data\_data.mdb;Persist Security Info=False;jet OLEDB:Database password=422127197509080072" cN.Open
a = Split(sql)
If InStr("insert,delete,updata", Trim(a(0))) Then cN.Execute sql Select Case Trim(a(0)) Case "insert" msgstring = "添加记录" Case "delete" msgstring = "删除记录" Case "updata" msgstring = "更新记录" End Select msgstring = msgstring & "已完成" Else
Set rst = New ADODB.Recordset rst.CursorLocation = adUseClient rst.Open sql, cN, adOpenKeyset, adLockOptimistic
Set chaxun = rst msgstring = "查询到" & rst.RecordCount & "条记录" End If
ExecuteSQL_Exit: Set rst = Nothing Set cN = Nothing Exit Function
ExecuteSQL_Error: msgstring = "查询错误: " & _ err.Description Resume ExecuteSQL_Exit End Function
Access 中是 OLE 对象的字段
Private Sub DBOpen()
'open the database with ADO
MYcon.Open "PROVIDER=MICROSOFT.JET.OLEDB.4.0; DATA SOURCE=" & App.Path + "/DBpic.MDB"
MYrs.Open "PICTABLE", MYcon, 1, 3
End Sub
'Close the open database
Private Sub DBClose()
MYrs.Close
MYcon.Close
Set MYrs = Nothing
Set MYcon = Nothing
End Sub
Private Sub SaveInto(ByVal strPath As String)
Dim lngFileLength As Long 'the length of the file
Dim lngBlockCount As Long 'the number of total whole block
Dim lngLastBlock As Integer 'the length of the last block
Dim lngBlockIndex As Long 'the index of each block
Dim ByteGet() As Byte '用于传送数据的二进制数组
Dim FileNum As Integer 'return the file number which the next file will use
Dim strFilepath As String
strFilepath = strPath
FileNum = FreeFile()
Open strFilepath For Binary Access Read As #FileNum
lngFileLength = LOF(FileNum) '返回一个 Long,表示用 Open 语句打开的文件的大小,该大小以字节为单位。
lngBlockCount = lngFileLength \ lngBlockSize
lngLastBlock = lngFileLength Mod lngBlockSize
MYrs.AddNew
MYrs.Fields("size") = lngFileLength
MYrs.Fields("date") = Date
MYrs.Fields("name") = Trim(Text1)
ReDim ByteGet(lngBlockSize)
For lngBlockIndex = 1 To lngBlockCount
Get #FileNum, , ByteGet()
MYrs.Fields("pic").AppendChunk ByteGet()
Next
If lngLastBlock > 0 Then
ReDim ByteGet(lngLastBlock)
Get #FileNum, , ByteGet()
MYrs.Fields("pic").AppendChunk ByteGet()
End If
MYrs.Update
Close #FileNum
End Sub
Private Sub ShowImg(ByVal RecordPoint As Long)
On Error Resume Next
Dim temp_path As String
Dim temp_file As String
Dim length As Long
Dim lngFileLength As Long 'the length of the file
Dim lngBlockCount As Long 'the number of total whole block
Dim lngLastBlock As Integer 'the length of the last block
Dim lngBlockIndex As Long 'the index of each block
Dim ByteGet() As Byte '用于传送数据的二进制数组
Dim FileNum As Integer 'return the file number which the next file will use
Dim strFileName As String
temp_path = Space$(MAX_PATH)
length = GetTempPath(MAX_PATH, temp_path)
temp_path = Left$(temp_path, length)
temp_file = Space$(MAX_PATH)
GetTempFileName temp_path, "per", 0, temp_file
strFileName = Left$(temp_file, InStr(temp_file, Chr$(0)) - 1)
MYrs.MoveFirst
MYrs.Move RecordPoint
Label1 = MYrs.AbsolutePosition
frmMain.Caption = MYrs.Fields("name") + Str(i)
FileNum = FreeFile()
Open strFileName For Binary As #FileNum
lngFileLength = MYrs.Fields("size")
lngBlockCount = lngFileLength \ lngBlockSize
lngLastBlock = lngFileLength Mod lngBlockSize
For lngBlockIndex = 1 To lngBlockCount
ByteGet() = MYrs.Fields("pic").GetChunk(lngBlockSize)
Put #FileNum, , ByteGet()
Next
If lngLastBlock > 0 Then
ReDim ByteGet(lngLastBlock)
ByteGet() = MYrs.Fields("pic").GetChunk(lngBlockSize)
Put #FileNum, , ByteGet()
End If
Picture1.Picture = LoadPicture(strFileName)
Close #FileNum
Kill strFileName
Err.Clear
End Sub
Private Sub AimFilePath(ByVal strPath As String)
Dim PathVal As String
PathVal = Dir(strPath)
If PathVal = Null Then MsgBox "null"
Do While PathVal <> ""
SaveInto (strPath + PathVal)
PathVal = Dir
Loop
End Sub
如果真要写入数据库,字段使用“ole对象“
On Error GoTo myerror
If quanxianjiancha("增加记录") = True Then
Dim xx As String
Dim yy As String
Dim rst As ADODB.Recordset
xx = "select 照片 from 照片库 where 序号=" & Val(Trim(ListView1.SelectedItem.ListSubItems(1).Text))
Set rst = chaxun(xx, yy)
If rst.RecordCount > 0 Then MsgBox "有照片,加什么?", , "警告": Exit Sub
Dim bit() As Byte
Dim varPath As String '图片的路径
CommonDialog1.Filter = "Pictures (*.bmp;*.jpg)|*.bmp;*.jpg"
CommonDialog1.ShowOpen
varPath = CommonDialog1.FileName
If varPath = "" Then Exit Sub
xx = "select * from 照片库"
Set rst = chaxun(xx, yy)
Open varPath For Binary As #1
ReDim bit(LOF(1)) As Byte
Get 1, 1, bit
Close 1
' 然后将字节数组的内容写入数据库即可
rst.AddNew
rst.Fields("照片").AppendChunk bit
rst.Fields("序号") = Val(Trim(ListView1.SelectedItem.ListSubItems(1).Text))
rst.Update
Image1.Picture = LoadPicture(varPath)
Slider1.Visible = True
asPopup5.Visible = True
asPopup2.Visible = True
asPopup3.Visible = True
asPopup4.Visible = False
ListView1.SelectedItem.ForeColor = vbBlue
Call WriteToIni(App.Path & "\set.dat", "有照片否", "序号" & Val(Trim(ListView1.SelectedItem.ListSubItems(1).Text)), 1)
zpshuliang = zpshuliang + 1
Label6.Caption = "照片总数: " & zpshuliang & " 张"
Set rst = NothingElse
MsgBox "无此权限", , "提示"
End If
Set rst = Nothing
myerror:
Exit Sub
End SubPrivate Sub huan_Click(Cancel As Boolean)
On Error GoTo myerror
If quanxianjiancha("修改记录") = True Then
Dim xx As String
Dim yy As String
Dim nrc As ADODB.Recordset
Dim bit() As Byte
Dim varPath As String '图片的路径
CommonDialog1.Filter = "Pictures (*.bmp;*.jpg)|*.bmp;*.jpg"
CommonDialog1.ShowOpen
varPath = CommonDialog1.FileName
If varPath = "" Then Exit Sub
xx = "select * from 照片库 where 序号=" & Val(Trim(ListView1.SelectedItem.ListSubItems(1).Text))
Set nrc = chaxun(xx, yy)
Open varPath For Binary As #1
ReDim bit(LOF(1)) As Byte
Get 1, 1, bit
Close 1
nrc.Fields("照片").AppendChunk bit
nrc.Update
Image1.Picture = LoadPicture(varPath)
Image1.RefreshElse
MsgBox "无此权限", , "提示"
End If
Set nrc = Nothing
Exit Submyerror:
MsgBox "更改不成功", , "提示"
Exit Sub
End SubPrivate Sub del_Click(Cancel As Boolean)
If quanxianjiancha("删除记录") = True Then
Dim xx As String
Dim yy As String
Dim rst As ADODB.Recordset
xx = "delete from 照片库 where 序号=" & Val(Trim(ListView1.SelectedItem.ListSubItems(1).Text))
Set rst = chaxun(xx, yy)
MsgBox yy
Image1.Picture = Nothing
ListView1.SelectedItem.ForeColor = vbRed
Call WriteToIni(App.Path & "\set.dat", "有照片否", "序号" & Val(Trim(ListView1.SelectedItem.ListSubItems(1).Text)), 0)
zpshuliang = zpshuliang - 1
Label6.Caption = "照片总数: " & zpshuliang & " 张"
Else
MsgBox "无此权限", , "提示"
End If
Set rst = Nothing
End Sub
Public Function chaxun(ByVal sql As String, msgstring As String) As ADODB.Recordset
On Error GoTo ExecuteSQL_Error
Dim cN As ADODB.Connection
Dim rst As ADODB.Recordset
Dim a() As String
Set cN = New ADODB.Connection
Dim str As String
str = App.Path
If Right(str, 1) <> "\" Then
str = str + "\"
End If cN.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\data\_data.mdb;Persist Security Info=False;jet OLEDB:Database password=422127197509080072"
cN.Open
a = Split(sql)
If InStr("insert,delete,updata", Trim(a(0))) Then
cN.Execute sql
Select Case Trim(a(0))
Case "insert"
msgstring = "添加记录"
Case "delete"
msgstring = "删除记录"
Case "updata"
msgstring = "更新记录"
End Select
msgstring = msgstring & "已完成"
Else
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseClient
rst.Open sql, cN, adOpenKeyset, adLockOptimistic
Set chaxun = rst
msgstring = "查询到" & rst.RecordCount & "条记录"
End If
ExecuteSQL_Exit:
Set rst = Nothing
Set cN = Nothing
Exit Function
ExecuteSQL_Error:
msgstring = "查询错误: " & _
err.Description
Resume ExecuteSQL_Exit
End Function