1,066
社区成员
发帖
与我相关
我的任务
分享
Private Function GetExtension(Filename As String) As String
Dim i, j, path, Ext As Integer
For i = Len(Filename) To 1 Step -1
If Mid(Filename, i, 1) = "." Then
Ext = i
Exit For
End If
Next i
If Ext = 0 Then
Exit Function
End If
GetExtension = Mid(Filename, Ext + 1, Len(Filename) - Ext)
End Function
Private Sub SaveToDB(nID As Long)
Dim cn As New ADODB.Connection
Dim Rst As New ADODB.Recordset
Dim Mstream As New ADODB.Stream
Dim sql As String
Dim MediaName As String
On Error GoTo err
MediaName = Trim$(txtPath.Text)
Set cn = GetConn
Rst.CursorLocation = adUseClient
sql = "select ID,SaveValue from tbFileManage where ID=" & nID
Rst.Open sql, cn, adOpenStatic, adLockPessimistic
Mstream.Type = adTypeBinary
Mstream.Open
Mstream.LoadFromFile MediaName
Rst.Fields("SaveValue").Value = Mstream.Read
Rst.Update
Rst.Close
Set Rst = Nothing
cn.Close
Set cn = Nothing
Set Mstream = Nothing
Exit Sub
err:
If Rst.State <> adStateClosed Then Rst.Close
Set Rst = Nothing
If cn.State <> adStateClosed Then cn.Close
Set cn = Nothing
Set Mstream = Nothing
End Sub
Private Sub ReadFromDB(nID As Long, sFullPath As String)
Dim cn As New ADODB.Connection
Dim Rst As New ADODB.Recordset
Dim Mstream As ADODB.Stream
Dim sql As String
Dim tmpFile As String
On Error GoTo err
Set cn = GetConn
sql = "select ID,SaveValue,FileType from tbFileManage where ID=" & nID
Rst.CursorLocation = adUseClient
Rst.Open sql, cn, adOpenStatic, adLockReadOnly
If IsNull(Rst.Fields("SaveValue").Value) Then
MsgBox "文档无内容", vbExclamation
Rst.Close: Set Rst = Nothing
cn.Close: Set cn = Nothing
Exit Sub
End If
Set Mstream = New ADODB.Stream
Mstream.Type = adTypeBinary
Mstream.Open
Mstream.Write Rst.Fields("SaveValue").Value
tmpFile = sFullPath & GetGUID & "." & Rst.Fields("FileType").Value
Mstream.SaveToFile tmpFile, adSaveCreateOverWrite
Mstream.Close
Set Mstream = Nothing
Rst.Close
cn.Close
Set cn = Nothing
Set Rst = Nothing
ShellExecute Me.hwnd, "Open", tmpFile, "", App.path, 1
Exit Sub
err:
If Rst.State <> adStateClosed Then Rst.Close
Set Rst = Nothing
If cn.State <> adStateClosed Then cn.Close
Set cn = Nothing
Set Mstream = Nothing
MsgBox err.Description
End Sub
Public Function ExtractFileExt(FileName As String) As String '返回文件扩展名
Dim i As Integer, L As Integer
i = InStrRev(FileName, ".")
L = Len(FileName)
If (L > i) And (i > 0) Then
ExtractFileExt = Right(FileName, L - i)
Else
ExtractFileExt = ""
End If
End Function
Public Function ExtractFilePath(FileName As String) As String '返回特定文件的路径
Dim i As Integer
i = InStrRev(FileName, "\")
If i > 0 Then
ExtractFilePath = Left(FileName, i)
Else
ExtractFilePath = ""
End If
End Function
Public Function ExtractFileName(FileName As String) As String '从全路径名中返回文件名
Dim i As Integer, L As Integer
i = InStrRev(FileName, "\")
L = Len(FileName)
If (L > i) And (i >= 0) Then
ExtractFileName = Right(FileName, L - i)
Else
ExtractFileName = ""
End If
End Function
Public Function ChangeFileExt(FileName As String, Ext As String) As String '改变文件扩展名
Dim i As Integer, L As Integer
i = InStrRev(FileName, ".")
L = Len(FileName)
If i > 0 Then
ChangeFileExt = Left(FileName, i - 1) & IIf(InStr(Ext, ".") > 0, Ext, "." & Ext)
Else
ChangeFileExt = FileName & IIf(InStr(Ext, ".") > 0, Ext, "." & Ext)
End If
End Function