找到过一个关于BLOB字段的读写模块,用这个来存取图片等文件都成功了,但是.TXT用这个来处理,想存进CLOB字段会变成乱码,有人能找出其中的问题所在吗?Option Explicit
Public rn As ADODB.ConnectionPublic Function CreateDataSource(DataSource As String, UserID As String, Password As String) As Boolean
On Error GoTo DbConErr:
Set rn = New ADODB.Connection
With rn
.ConnectionString = "Provider=OraOLEDB.Oracle.1; password=TEST ;User ID = TEST;Data Source=DEV;Locale Identifier=2052"
.Open
End With
CreateDataSource = True
Exit Function
DbConErr:
CreateDataSource = False
End FunctionSub BlobToFile(fld As ADODB.Field, filename As String, Optional ChunkSize As Long = 8192)
    Dim fnum As Integer, bytesleft As Long, bytes As Long
    Dim tmp() As Byte
    If (fld.Attributes And adFldLong) = 0 Then
        Err.Raise 1001, , "field doesn't support the GetChunk method."
    End If
    If Dir$(filename) <> "" Then Kill filename
    fnum = FreeFile
    Open filename For Binary As fnum
    bytesleft = fld.ActualSize
    Do While bytesleft
        bytes = bytesleft
        If bytes > ChunkSize Then bytes = ChunkSize
        tmp = fld.GetChunk(bytes)
        Put #fnum, , tmp
        bytesleft = bytesleft - bytes
    Loop
    Close #fnum
End SubSub FileToBlob(fld As ADODB.Field, filename As String, Optional ChunkSize As Long = 8192)
    Dim fnum As Integer, bytesleft As Long, bytes As Long
    Dim tmp() As Byte
    If (fld.Attributes And adFldLong) = 0 Then
        Err.Raise 1001, , "field doesn't support the GetChunk method."
    End If
    If Dir$(filename) = "" Then Err.Raise 53, , "File not found"
    fnum = FreeFile
    Open filename For Binary As fnum
    bytesleft = LOF(fnum)
    Do While bytesleft
        bytes = bytesleft
        If bytes > ChunkSize Then bytes = ChunkSize
        ReDim tmp(1 To bytes) As Byte
        Get fnum, , tmp
        fld.AppendChunk tmp
        bytesleft = bytesleft - bytes
    Loop
    Close #fnum
End SubPrivate Sub cmdread_Click()
    Dim rs As New ADODB.Recordset
    rs.ActiveConnection = rn
    rs.LockType = adLockOptimistic
    rs.CursorLocation = adUseClient
    rs.Source = "select * from t_demo"
    rs.Open
    comDlgDir.DialogTitle = "保存文件"
    comDlgDir.Filter = "*.*"
    comDlgDir.ShowSave
    Call BlobToFile(rs.Fields("text"), comDlgDir.filename)
    Set rs = Nothing
    Exit Sub
    Set rs = Nothing
End Sub
Private Sub cmdsave_Click()
    Dim rs As New ADODB.Recordset
    rs.ActiveConnection = rn
    rs.LockType = adLockOptimistic
    rs.CursorLocation = adUseClient
    rs.Source = "select * from t_demo"
    rs.Open 
    rs.AddNew
    comDlgDir.DialogTitle = "选取文件"
    comDlgDir.ShowOpen
    rs.Fields("id").Value = 1
    If comDlgDir.filename <> "" Then
        Call FileToBlob(rs.Fields("text"), comDlgDir.filename)
        rs.Update
    End If
    Set rs = Nothing
    Exit Sub
    Set rs = Nothing
End Sub
Private Sub Form_Load()
    If Not CreateDataSource("DEV", "TEST", "TEST") Then
        MsgBox "Connection failure!"
    End If
End Sub