- Public Function FileToRecode(ByRef P_Cnn As ADODB.Connection, _
- TabName As String, _
- FldName As String, _
- WhereStr As String, _
- Filename As String) As Boolean
-
- Dim RsB As New ADODB.Recordset
- Dim Person_name As String
- Dim StrSql As String
- Dim File_Num As String
- Dim File_Length As String
- Dim Bytes() As Byte
- Dim Num_Blocks As Long
- Dim Left_Over As Long
- Dim Block_Num As Long
-
- Err.Clear
- On Error Resume Next
-
- File_Num = FreeFile
- Filename = Trim$(Filename)
-
- If P_Cnn.State <> 1 Then P_Cnn.Open
-
- If Len(Dir$(Filename)) = 0 Or Len(Filename) = 0 Then FileToRecode = False: Exit Function
-
- Open Filename For Binary Access Read As #File_Num
- File_Length = LOF(File_Num)
- If File_Length > 0 Then
- Num_Blocks = File_Length / Block_Size
- Left_Over = File_Length Mod Block_Size
-
- If Left$(Trim$(UCase$(WhereStr)), Len("where")) <> UCase$("where") Then WhereStr = "Where " & Trim$(WhereStr)
- StrSql = "Select Top 1 [" & TabName & "].[" & FldName & "] From [" & TabName & "] " & WhereStr
- Set RsB = RsOpen(P_Cnn, StrSql, False)
- If Not (RsB.EOF And RsB.BOF) Then
-
-
- ReDim Bytes(Block_Size)
- For Block_Num = 1 To Num_Blocks
- Get #File_Num, , Bytes()
- RsB.Fields(FldName).AppendChunk Bytes()
- Next
-
- If Left_Over > 0 Then
- ReDim Bytes(Left_Over)
- Get #File_Num, , Bytes()
- RsB.Fields(FldName).AppendChunk Bytes()
- End If
- RsB.Update
- DoEvents
- End If
- If RsB.State = adStateOpen Then
- RsB.Close
- Set RsB = Nothing
- End If
- End If
- Close #File_Num
- Erase Bytes
- FileToRecode = (Err.Number = 0)
- Err.Clear
- End Function
- Public Function RecodeToFile(ByRef P_Cnn As ADODB.Connection, _
- TabName As String, _
- FldName As String, _
- WhereStr As String, _
- Optional FileType As String = "Bmp") As String
-
- Dim Rs As New ADODB.Recordset
- Dim StrSql As String
-
- Dim Bytes() As Byte
- Dim File_Name As String
- Dim File_Num As Integer
- Dim File_Length As Long
- Dim Num_Blocks As Long
- Dim Left_Over As Long
- Dim Block_Num As Long
- Dim WorkPath As String
- Dim TmpDir As New SmSysCls
-
- Err.Clear
- On Error Resume Next
-
- WorkPath = TmpDir.GetFolder(SmWinTempDirectory)
- If Dir$(WorkPath, vbDirectory) = "" Then WorkPath = App.Path
- If Right$(WorkPath, 1) <> "\" Then WorkPath = WorkPath & "\"
-
- If Left$(Trim$(UCase$(WhereStr)), Len("where")) <> UCase$("where") Then WhereStr = "Where " & Trim$(WhereStr)
- StrSql = "Select Top 1 [" & TabName & "].[" & FldName & "] From [" & TabName & "] " & WhereStr
- Set Rs = RsOpen(P_Cnn, StrSql)
- If Rs.BOF And Rs.EOF Then Exit Function
-
- If P_Cnn.State <> 1 Then P_Cnn.Open
-
- If Not IsNull(Rs.Fields(FldName)) Then
- File_Name = WorkPath & "TmpFile." & FileType
- If Len(Dir(File_Name)) <> 0 Then Kill File_Name
- File_Num = FreeFile
- Open File_Name For Binary As #File_Num
- File_Length = CT.ToLng(Rs.Fields(FldName).ActualSize)
- Num_Blocks = File_Length \ Block_Size
- Left_Over = File_Length Mod Block_Size
- For Block_Num = 1 To Num_Blocks
- Bytes() = Rs.Fields(FldName).GetChunk(Block_Size)
- Put #File_Num, , Bytes()
- Next
- If Left_Over > 0 Then
- Bytes() = Rs.Fields(FldName).GetChunk(Left_Over)
- Put #File_Num, , Bytes()
- End If
- Erase Bytes
- Close #File_Num
-
- If Rs.State = adStateOpen Then
- Rs.Close
- Set Rs = Nothing
- End If
-
- Erase Bytes
- End If
- RecodeToFile = IIf(Err.Number = 0, File_Name, "")
- Set TmpDir = Nothing
- Err.Clear
- End Function
发表于 @
2008年09月16日 08:34:00 | | 编辑|
举报| 收藏