Option ExplicitPrivate Const ConnStr = "Provider=SQLOLEDB;Data Source=ZKJC-WW\xCas;User ID=sa;Password=sa;Initial Catalog=tempdb;Persist Security Info=True"' Create Table Simple( ' ID int IDENTITY(1,1) Primary Key, ' Information varchar(100) NULL, ' Data Image NULL ' ) ' Private Function SaveImage(FilePath As String) As Boolean On Error GoTo ErrHandler Dim Cnn As New ADODB.Connection, rsTmp As ADODB.Recordset Dim nFileNum As Long, nFileLen As Long, B() As Byte Dim sqlStr As String, nID As Long SaveImage = False If FilePath = "" Then Exit Function With Cnn .CursorLocation = adUseClient .ConnectionTimeout = 30 .Open ConnStr End With If Cnn.State <> 1 Then Exit Function nFileNum = FreeFile Open FilePath For Binary Access Read As #nFileNum nFileLen = LOF(nFileNum) '!!! File Size MUST Not ... ReDim B(1 To nFileLen) Get nFileNum, , B Close nFileNum' Save it sqlStr = "Insert Into Simple(Information) Values('')" Cnn.Execute sqlStr sqlStr = "Select ID,Data From Simple Order by ID DESC" Set rsTmp = New ADODB.Recordset rsTmp.Open sqlStr, Cnn, adOpenStatic, adLockOptimistic rsTmp(1).AppendChunk B() rsTmp.Update rsTmp.Close Cnn.Close SaveImage = True Exit Function ErrHandler: SaveImage = False End FunctionPrivate Function LoadImage(nID As Long, SavePath As String) As Boolean On Error GoTo ErrHandler Dim Cnn As New ADODB.Connection, rsTmp As ADODB.Recordset Dim sqlStr As String, B() As Byte Dim nFileNum As Long LoadImage = False With Cnn .CursorLocation = adUseClient .ConnectionTimeout = 30 .Open ConnStr End With If Cnn.State <> 1 Then Exit Function sqlStr = "Select ID,Data From Simple Where ID = " & nID Set rsTmp = New ADODB.Recordset rsTmp.Open sqlStr, Cnn, adOpenStatic, adLockOptimistic B() = rsTmp(1).GetChunk(rsTmp(1).ActualSize) rsTmp.Close Cnn.Close nFileNum = FreeFile Open SavePath For Binary Access Write As #nFileNum Put nFileNum, , B Close nFileNum Exit Function ErrHandler: LoadImage = False End FunctionPrivate Sub Form_Load() Call SaveImage("G:\1.jpg") Call LoadImage(1, "G:\2.jpg") End Sub
' ID int IDENTITY(1,1) Primary Key,
' Information varchar(100) NULL,
' Data Image NULL
' )
'
Private Function SaveImage(FilePath As String) As Boolean
On Error GoTo ErrHandler
Dim Cnn As New ADODB.Connection, rsTmp As ADODB.Recordset
Dim nFileNum As Long, nFileLen As Long, B() As Byte
Dim sqlStr As String, nID As Long
SaveImage = False
If FilePath = "" Then Exit Function
With Cnn
.CursorLocation = adUseClient
.ConnectionTimeout = 30
.Open ConnStr
End With
If Cnn.State <> 1 Then Exit Function
nFileNum = FreeFile
Open FilePath For Binary Access Read As #nFileNum
nFileLen = LOF(nFileNum) '!!! File Size MUST Not ...
ReDim B(1 To nFileLen)
Get nFileNum, , B
Close nFileNum' Save it
sqlStr = "Insert Into Simple(Information) Values('')"
Cnn.Execute sqlStr
sqlStr = "Select ID,Data From Simple Order by ID DESC"
Set rsTmp = New ADODB.Recordset
rsTmp.Open sqlStr, Cnn, adOpenStatic, adLockOptimistic
rsTmp(1).AppendChunk B()
rsTmp.Update
rsTmp.Close
Cnn.Close
SaveImage = True
Exit Function
ErrHandler:
SaveImage = False
End FunctionPrivate Function LoadImage(nID As Long, SavePath As String) As Boolean
On Error GoTo ErrHandler
Dim Cnn As New ADODB.Connection, rsTmp As ADODB.Recordset
Dim sqlStr As String, B() As Byte
Dim nFileNum As Long
LoadImage = False
With Cnn
.CursorLocation = adUseClient
.ConnectionTimeout = 30
.Open ConnStr
End With
If Cnn.State <> 1 Then Exit Function
sqlStr = "Select ID,Data From Simple Where ID = " & nID
Set rsTmp = New ADODB.Recordset
rsTmp.Open sqlStr, Cnn, adOpenStatic, adLockOptimistic
B() = rsTmp(1).GetChunk(rsTmp(1).ActualSize)
rsTmp.Close
Cnn.Close
nFileNum = FreeFile
Open SavePath For Binary Access Write As #nFileNum
Put nFileNum, , B
Close nFileNum
Exit Function
ErrHandler:
LoadImage = False
End FunctionPrivate Sub Form_Load()
Call SaveImage("G:\1.jpg")
Call LoadImage(1, "G:\2.jpg")
End Sub