Option ExplicitDim byteArray() As BytePrivate Sub cmdBrowse_Click()
Me.CommonDialog1.ShowOpen
If Len(Me.CommonDialog1.FileName) > 0 Then
Me.txtPath.Text = Me.CommonDialog1.FileName
End If
End SubPrivate Sub cmdDownload_Click()
Dim oConn As ADODB.Connection
Dim oCmd As ADODB.Command
Dim oRS As ADODB.Recordset
Dim strPath As String
Dim i As Long
Dim fldPhoto As ADODB.Field
Dim varData() As Byte
On Error GoTo errHandler
i = InputBox("Enter record ID")
Set oConn = New ADODB.Connection
oConn.Open "Driver={SQL Server};Server=websvr;uid=sa;password=MSSQLSERVER;Database=demo"
Set oRS = New ADODB.Recordset
oRS.Open "SELECT * FROM blobtest WHERE id = " & i, oConn, adOpenKeyset, adLockOptimistic
Set fldPhoto = oRS("photo")
varData = fldPhoto.GetChunk(fldPhoto.ActualSize)
oRS.Close
oConn.Close
Set oRS = Nothing
Set oConn = Nothing
strPath = "c:\test.jpg"
SaveToFile strPath, varData
Image1.Picture = LoadPicture(strPath)
Exit SuberrHandler:
MsgBox "Oops! We had an error." & vbCrLf & "Error &H" & Hex(Err) & " - " & vbCrLf & Err.Description, vbInformation
Exit Sub
End SubPrivate Sub cmdUpload_Click()
Dim oConn As ADODB.Connection
Dim oCmd As ADODB.Command
Dim oRS As ADODB.Recordset
Dim strPath As String
strPath = txtPath.Text
If Len(Dir(strPath)) = 0 Then
txtPath.SetFocus
Exit Sub
End If
On Error GoTo errHandler
Set oConn = New ADODB.Connection
oConn.Open "Driver={SQL Server};Server=websvr;uid=sa;password=MSSQLSERVER;Database=demo"
Set oRS = New ADODB.Recordset
oRS.Open "blobtest", oConn, adOpenKeyset, adLockOptimistic
oRS.AddNew
If GetFileData(strPath) Then
oRS("photo").AppendChunk byteArray
oRS.Update
End If
oRS.Close
oConn.Close
Set oRS = Nothing
Set oConn = Nothing
Exit SuberrHandler:
MsgBox "Oops! We had an error." & vbCrLf & "Error &H" & Hex(Err) & " - " & vbCrLf & Err.Description, vbInformation
Exit Sub
End SubFunction GetFileData(ByVal strPath As String) As Boolean
Dim hFileHandle
ReDim byteArray(FileLen(strPath))
hFileHandle = FreeFile
Open strPath For Binary Access Read Lock Write As #hFileHandle
Get #hFileHandle, , byteArray
GetFileData = True
Close #hFileHandle
End FunctionFunction SaveToFile(ByVal strPath As String, varData() As Byte) As Boolean
Dim hFileHandle As Long
hFileHandle = FreeFile
Open strPath For Binary Access Write Lock Write As #hFileHandle
Put #hFileHandle, , varData
Close #hFileHandle
SaveToFile = True
End Function
Me.CommonDialog1.ShowOpen
If Len(Me.CommonDialog1.FileName) > 0 Then
Me.txtPath.Text = Me.CommonDialog1.FileName
End If
End SubPrivate Sub cmdDownload_Click()
Dim oConn As ADODB.Connection
Dim oCmd As ADODB.Command
Dim oRS As ADODB.Recordset
Dim strPath As String
Dim i As Long
Dim fldPhoto As ADODB.Field
Dim varData() As Byte
On Error GoTo errHandler
i = InputBox("Enter record ID")
Set oConn = New ADODB.Connection
oConn.Open "Driver={SQL Server};Server=websvr;uid=sa;password=MSSQLSERVER;Database=demo"
Set oRS = New ADODB.Recordset
oRS.Open "SELECT * FROM blobtest WHERE id = " & i, oConn, adOpenKeyset, adLockOptimistic
Set fldPhoto = oRS("photo")
varData = fldPhoto.GetChunk(fldPhoto.ActualSize)
oRS.Close
oConn.Close
Set oRS = Nothing
Set oConn = Nothing
strPath = "c:\test.jpg"
SaveToFile strPath, varData
Image1.Picture = LoadPicture(strPath)
Exit SuberrHandler:
MsgBox "Oops! We had an error." & vbCrLf & "Error &H" & Hex(Err) & " - " & vbCrLf & Err.Description, vbInformation
Exit Sub
End SubPrivate Sub cmdUpload_Click()
Dim oConn As ADODB.Connection
Dim oCmd As ADODB.Command
Dim oRS As ADODB.Recordset
Dim strPath As String
strPath = txtPath.Text
If Len(Dir(strPath)) = 0 Then
txtPath.SetFocus
Exit Sub
End If
On Error GoTo errHandler
Set oConn = New ADODB.Connection
oConn.Open "Driver={SQL Server};Server=websvr;uid=sa;password=MSSQLSERVER;Database=demo"
Set oRS = New ADODB.Recordset
oRS.Open "blobtest", oConn, adOpenKeyset, adLockOptimistic
oRS.AddNew
If GetFileData(strPath) Then
oRS("photo").AppendChunk byteArray
oRS.Update
End If
oRS.Close
oConn.Close
Set oRS = Nothing
Set oConn = Nothing
Exit SuberrHandler:
MsgBox "Oops! We had an error." & vbCrLf & "Error &H" & Hex(Err) & " - " & vbCrLf & Err.Description, vbInformation
Exit Sub
End SubFunction GetFileData(ByVal strPath As String) As Boolean
Dim hFileHandle
ReDim byteArray(FileLen(strPath))
hFileHandle = FreeFile
Open strPath For Binary Access Read Lock Write As #hFileHandle
Get #hFileHandle, , byteArray
GetFileData = True
Close #hFileHandle
End FunctionFunction SaveToFile(ByVal strPath As String, varData() As Byte) As Boolean
Dim hFileHandle As Long
hFileHandle = FreeFile
Open strPath For Binary Access Write Lock Write As #hFileHandle
Put #hFileHandle, , varData
Close #hFileHandle
SaveToFile = True
End Function
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货