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, 3End Sub
'Close the open database
Private Sub DBClose()
MYrs.Close
MYcon.Close
Set MYrs = Nothing
Set MYcon = Nothing
End SubPrivate 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 SubPrivate Sub ShowImg(ByVal RecordPoint As Long)
On Error Resume NextDim 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 Stringtemp_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 SubPrivate 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
'open the database with ADO
MYcon.Open "PROVIDER=MICROSOFT.JET.OLEDB.4.0; DATA SOURCE=" & App.Path + "/DBpic.MDB"
MYrs.Open "PICTABLE", MYcon, 1, 3End Sub
'Close the open database
Private Sub DBClose()
MYrs.Close
MYcon.Close
Set MYrs = Nothing
Set MYcon = Nothing
End SubPrivate 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 SubPrivate Sub ShowImg(ByVal RecordPoint As Long)
On Error Resume NextDim 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 Stringtemp_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 SubPrivate 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
解决方案 »
- image click事件加上这一句代码后反映速度会出问题?
- MSFlexGrid 中列的宽度怎么能够让它随内容的宽度自动变化
- 使用RichTextBox控件时,如何屏蔽掉(Ctrl+c)、(Ctrl+v)键盘热键。
- 全局变量怎么用啊?求助!!!
- 一个简单的问题
- 请问如何将Integer转换成string啊???
- 关于读取文件输出到text控件的问题?
- 我有vs.net正版7cd谁要![email protected]
- 散分!专家分超100了!
- 怎样写代码执行WebBroswer控件菜单中的“Print”命令呢?
- ActiveReport预览正常,打印错位、漏打问题。万分火急,给100分
- 如何实现随机变换颜色??
ado2.6以上的都可以!
不过我只会VB!
'Insert into SQL Server
Private Sub ImportBLOB(cn As ADODB.Connection)
Dim rs As New ADODB.Recordset
Dim stm As ADODB.Stream
Set stm = New ADODB.Stream
' Skip any table not found errors
On Error Resume Next
cn.Execute "drop table BinaryObject"
On Error GoTo 0
'Create the BinaryObject table
cn.Execute "create table BinaryObject " & _
"(blob_id int IDENTITY(1,1), " & _
"blob_filename varchar(256), " & _
"blob_object image)"
rs.Open "Select * from BinaryObject where 1=2", cn, adOpenKeyset, adLockOptimistic
'Read the binary files from disk
stm.Type = adTypeBinary
stm.Open
stm.LoadFromFile App.Path & "\BLOBsample.jpg"
rs.AddNew
rs!blob_filename = App.Path & "\BLOBsample.jpg"
rs!blob_object = stm.Read
'Insert the binary object in the table
rs.Update
rs.Close
stm.Close
Set rs = Nothing
Set stm = Nothing
End Sub
Private Sub DisplayBLOB(cn As ADODB.Connection) Dim rs As New ADODB.Recordset
' Select the only image in the table
rs.Open "Select * from BinaryObject where blob_id = 1", cn
' Set the DataSource to the recordset
Set imgBinaryData.DataSource = rs
'Set the DataField to the BLOB field
imgBinaryData.DataField = rs!blob_object.Name
'Release the recordset
rs.Close
Set rs = NothingEnd Sub
Dim Auno As String '车辆号牌号码
Dim Row As Long '页码号
Dim FileName As String '
Dim stm As New ADODB.Stream '二进制流
Timer1.Enabled = False
While Not L_rs.EOF
Auno = L_rs!Auno
Row = L_rs!inrow
FileName = L_rs!fpath & L_rs!fname
If stm.State <> adStateClosed Then
stm.Close
End If
stm.Type = adTypeBinary
stm.Open
stm.LoadFromFile FileName
G_Rs.AddNew
G_Rs.Fields("Auno") = Trim(Auno)
G_Rs.Fields("InRow") = Row
G_Rs.Fields("APos") = Pos
G_Rs.Fields("PageN").AppendChunk stm.Read
G_Rs.Update
stm.Close
L_rs.MoveNext
Wend
'////读出
While Not G_Rs1.EOF
Set L_rs = G_Conn.Execute("select * from Au_vehicles where Auno='" & G_Rs1!c_hphm & "' order by inrow asc")
While Not L_rs.EOF
If stm.State <> adStateClosed Then
stm.Close
End If
stm.Type = adTypeBinary
stm.Open
stm.Write L_rs.Fields("PageN").GetChunk(L_rs.Fields("PageN").ActualSize)
stm.SaveToFile TmpPath & "\tmp" & G_Rs1!c_hphm & L_rs!inrow & ".jpg", IIf(Len(Trim(Dir(TmpPath & "\tmp" & G_Rs1!c_hphm & L_rs!inrow & ".jpg", vbNormal + vbHidden))) > 0, adSaveCreateOverWrite, adSaveCreateNotExist)
Dim st As String
st = "insert into Au_temp values ('" & L_rs!Auno & "','" & TmpPath & "\','" & "tmp" & G_Rs1!c_hphm & Trim(Str(L_rs!inrow)) & ".jpg'," & L_rs!inrow & ")"
G_Conn.Execute (st)
L_rs.MoveNext
Wend
G_Rs1.MoveNext
Wend
Sub SavePictureToAdodc(rs As ADODB.Recordset, ByVal FileName As String)
Dim Length As Long, f As Integer
Length = FileLen(FileName)
ReDim bArray(Length + 12) As Byte, bArray2(Length) As Byte
bArray(0) = &H6C: bArray(1) = &H74
RtlMoveMemory bArray(4), Length, 4
f = FreeFile
Open FileName For Binary As #f
Get #f, , bArray2
Close #1
RtlMoveMemory bArray(8), bArray2(0), Length
rs("相片").AppendChunk bArray
End Sub
调用:
Private Sub Label2_Click()
On Error Resume NextWith CommonDialog1
.CancelError = True
.ShowOpen
If Err.Number <> cdlCancel Then Image2.Picture = LoadPicture(.FileName)
SavePictureToAdodc Adodc1.Recordset, .FileNameEnd If
End WithEnd Sub