存入数据乱码有关问题,请帮着看一下
存入数据乱码问题,请帮着看一下
这是源码 选取一个文件插入到库中库中对应的字段为“text”clob类型,插入以后为乱码。文件的内容无论是英文还是汉字都是乱码。尝试过文件另存为不同的编码类型Unicode Utf-8 ansi 都试过了还是乱码……
Dim rs As New ADODB.Recordset 'New a record set For result
rs.ActiveConnection = rn 'Active Connection
rs.LockType = adLockOptimistic
rs.CursorLocation = adUseClient
rs.Source = "select * from lawtest "
rs.Open
ComDlgDir.DialogTitle = "保存文件 " '
ComDlgDir.Filter = "*.* "
ComDlgDir.ShowSave
Call ClobToFile(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 lawtest "
rs.Open
rs.AddNew
ComDlgDir.DialogTitle = "选取文件 "
ComDlgDir.ShowOpen
rs.Fields( "lawtestid ").Value = 6
If ComDlgDir.Filename <> " " Then
'conStr = StrConv(rs.Fields( "text "), vbUnicode)
Call FileToClob(rs.Fields( "text "), ComDlgDir.Filename)
MsgBox rs.Fields( "text ") '可以看一下Fields( "text ")
rs.Update
End If
Set rs = Nothing
Exit Sub
Set rs = Nothing
End Sub
Sub FileToClob(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 'Open file
bytesleft = LOF(fnum) 'return the size of file
Do While bytesleft
bytes = bytesleft
If bytes > ChunkSize Then bytes = ChunkSize
ReDim tmp(1 To bytes) As Byte 'Dim a Byte Array
tmp = StrConv(tmp, vbFromUnicode)
Get fnum, , tmp
fld.AppendChunk tmp
bytesleft = bytesleft - bytes
Loop
MsgBox tmp 'tmp 一个用于临时存储的数组 长度固定8192
Close #fnum
End Sub
------解决方案--------------------
这是源码 选取一个文件插入到库中库中对应的字段为“text”clob类型,插入以后为乱码。文件的内容无论是英文还是汉字都是乱码。尝试过文件另存为不同的编码类型Unicode Utf-8 ansi 都试过了还是乱码……
Dim rs As New ADODB.Recordset 'New a record set For result
rs.ActiveConnection = rn 'Active Connection
rs.LockType = adLockOptimistic
rs.CursorLocation = adUseClient
rs.Source = "select * from lawtest "
rs.Open
ComDlgDir.DialogTitle = "保存文件 " '
ComDlgDir.Filter = "*.* "
ComDlgDir.ShowSave
Call ClobToFile(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 lawtest "
rs.Open
rs.AddNew
ComDlgDir.DialogTitle = "选取文件 "
ComDlgDir.ShowOpen
rs.Fields( "lawtestid ").Value = 6
If ComDlgDir.Filename <> " " Then
'conStr = StrConv(rs.Fields( "text "), vbUnicode)
Call FileToClob(rs.Fields( "text "), ComDlgDir.Filename)
MsgBox rs.Fields( "text ") '可以看一下Fields( "text ")
rs.Update
End If
Set rs = Nothing
Exit Sub
Set rs = Nothing
End Sub
Sub FileToClob(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 'Open file
bytesleft = LOF(fnum) 'return the size of file
Do While bytesleft
bytes = bytesleft
If bytes > ChunkSize Then bytes = ChunkSize
ReDim tmp(1 To bytes) As Byte 'Dim a Byte Array
tmp = StrConv(tmp, vbFromUnicode)
Get fnum, , tmp
fld.AppendChunk tmp
bytesleft = bytesleft - bytes
Loop
MsgBox tmp 'tmp 一个用于临时存储的数组 长度固定8192
Close #fnum
End Sub
------解决方案--------------------