none
VB2008导入txt数据到access数据库后出现中文乱码应该如何解决? RRS feed

  • 问题

  • txt文档中有中文,导入access之后就变成乱码了。

    一下是我的程序代码:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myData As String
            Dim FilePath As String
            FilePath = myNewFullPath
            Dim DbPath As String
            DbPath = "E:\test.mdb"
            Dim conn As New Data.OleDb.OleDbConnection
            conn.ConnectionString = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DbPath & ";Jet OLEDB:Database Password=")
            Dim strSQL As String
            strSQL = "Select * Into myTableName From [Text;FMT=Delimited;HDR=Yes;DataBase=E:\].test.txt"
            On Error GoTo showerrstr
            conn.Open()
            If ConnectionState.Open Then
                Dim myCMD As New OleDb.OleDbCommand(strSQL, conn)
                myCMD.ExecuteNonQuery()
                MsgBox("导入已完成!", MsgBoxStyle.Information, "成功")
            End If
            conn.Close()
            Exit Sub
    showerrstr:
            MsgBox(Err.Number & ":" & Err.Description)
            conn.Close()
            Exit Sub
        End Sub
    '---------------以下是schema.ini---------------
    [test.txt]
    ColNameHeader=True
    format=Delimited(|)
    MaxScanRows=0
    CharacterSet=ANSI
    Col1="字段一" Char Width 30
    Col2="字段二" Char Width 30
    Col3="字段三" LongChar Width 50
    Col4="字段四" LongChar Width 50

    在网上搜索了很久找不到解决办法,请大家不吝赐教!
    2013年9月10日 14:07

答案

  • 关键代码:

    strSQL = "Select * Into IP From [Text;FMT=Delimited;HDR=Yes;DataBase=" & myFilePath & "]." & myNewFileName
    Dim file As New System.IO.StreamReader(myFullPath, System.Text.Encoding.GetEncoding("GB2312"))
    System.Text.Encoding.GetEncoding("GB2312")这句是关键

    2013年9月17日 8:06

全部回复

  • 你好:

    请问你的Access是哪个版本的,是简体中文版本的吗?

    还有就是你的文本文件的编码是什么时ANSI吗?你把它另存为Unicode编码试试看。或者把修改CharacterSet=Unicode看看。

    最好把编码都统一为Unicode编码。


    Caillen
    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月11日 10:21
    版主
  • 关键代码:

    strSQL = "Select * Into IP From [Text;FMT=Delimited;HDR=Yes;DataBase=" & myFilePath & "]." & myNewFileName
    Dim file As New System.IO.StreamReader(myFullPath, System.Text.Encoding.GetEncoding("GB2312"))
    System.Text.Encoding.GetEncoding("GB2312")这句是关键

    2013年9月17日 8:06