|
|
Хранение файлов в поле MEMO таблицы БД (ADO)
Private Const cnsTableName$ = "a00Files"
Private Const cnsRecordIDField = "FileID"
Private Const cnsFileNameField = "flName"
Private Const cnsFileBodyField = "flBody"
Private Const cnsFileLenField = "flLen"
Private Sub TestFilesInDB()
Dim sVal$, lErr&
lErr = FilesInDBImportOne(1, "d:\Temp\logo_clinic_360_420_v002.gif")
If lErr > 0 Then Debug.Print lErr
sVal = CurrentProject.Path & AConSubAppFolder
PrepareFoldersForPath sVal
lErr = FilesInDBExportOne(1, sVal)
If lErr > 0 Then Debug.Print lErr
End Sub
Public Function FilesInDBExportOne(lFileID As Long, ByVal sFolderPath As String, _
Optional bReplase As Boolean, Optional sNewFileName As String = "") As Long
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sVal As String
Dim vFileBody As Variant
Dim sFileName$
On Error GoTo FilesInDBExportOne_Err
Set cnt = CurrentProject.Connection
Set rst = CreateObject("ADODB.Recordset")
sVal = "SELECT * FROM " & cnsTableName & " WHERE (" & cnsRecordIDField & " = " & lFileID & ")"
rst.Open sVal, cnt, adOpenStatic, adLockReadOnly
If rst.EOF = True Then GoTo FilesInDBExportOne_Exit
vFileBody = rst.Fields(cnsFileBodyField)
If IsNull(cnsFileBodyField) = True Then GoTo FilesInDBExportOne_Exit
If sNewFileName = "" Then
sFileName = rst.Fields(cnsFileNameField)
Else
sFileName = sNewFileName
End If
If Not Right(sFolderPath, 1) = "\" Then sFolderPath = sFolderPath & "\"
sVal = sFolderPath & sFileName
If Not Dir(sVal) = "" Then
If bReplase = True Then
Kill sFolderPath
DoEvents
Else
GoTo FilesInDBExportOne_Exit
End If
End If
Reset
Open sVal For Output As #1
Print #1, vFileBody;
FilesInDBExportOne_Exit:
On Error Resume Next
Close #1
DoEvents
rst.Close: Set rst = Nothing
cnt.Close: Set cnt = Nothing
Err.Clear
Exit Function
FilesInDBExportOne_Err:
FilesInDBExportOne = Err.Number
MsgBox "Error " & Err.Number & vbCrLf & Err.Description & vbCrLf & _
"in procedure FilesInDBExportOne of Module [modFilesInDB_ADODB]", vbCritical, "Error!"
Err.Clear
Resume FilesInDBExportOne_Exit
End Function
Public Function FilesInDBImportOne(lFileID As Long, sFilePath$)
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim vFileBody As Variant
Dim sFileName$
Dim sVal As String, lFileLen&
On Error GoTo FilesInDBImportOne_Err
If Dir(sFilePath, vbNormal) = "" Then
sVal = "Файл:" & vbCrLf & sFilePath & vbCrLf & "Не найден!" & vbCrLf & _
"Ошибка в Function FilesInDBImportOne - modFilesInDB_ADODB."
MsgBox sVal, vbCritical, "Произошла ошибка!"
GoTo FilesInDBImportOne_End
End If
lFileLen = FileLen(sFilePath)
sFileName = GetFileNameByPath(sFilePath)
Set cnt = CurrentProject.Connection
Set rst = CreateObject("ADODB.Recordset")
sVal = "SELECT * FROM " & cnsTableName & " WHERE (" & cnsRecordIDField & " = " & lFileID & ")"
rst.Open sVal, cnt, adOpenDynamic, adLockOptimistic
If rst.EOF = True Then
rst.AddNew
rst(cnsRecordIDField) = lFileID
End If
Reset
Open sFilePath For Binary Access Read Lock Read As #1
vFileBody = Input(lFileLen, #1)
rst.Fields(cnsFileNameField) = sFileName
rst.Fields(cnsFileBodyField) = vFileBody
rst.Fields(cnsFileLenField) = lFileLen
rst.Update
FilesInDBImportOne_End:
On Error Resume Next
Close #1
DoEvents
rst.Close: Set rst = Nothing
cnt.Close: Set cnt = Nothing
Err.Clear
Exit Function
FilesInDBImportOne_Err:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Function" & _
"FilesInDBImportOne - modFilesInDB_ADODB.", vbCritical, "Произошла ошибка!"
Err.Clear
Resume FilesInDBImportOne_End
End Function
|
|