da.Fill(dt)在使用odbc适配器加载.csv时抛出错误

时间:2014-01-16 20:17:30

标签: vb.net csv datatable odbc

错误是:

The Microsoft Jet database engine could not find the object `FileName.csv` Make sure the object exists and that you spell its name and the path name correctly.

我正在使用vb.net来确定路径和名称,这不是问题。

我的代码:

 Public Sub ConvertCSV()
    Dim DirectoryPath, FileName, FileNameAndPath As String
    Dim openFileDialog1 As New OpenFileDialog()

    openFileDialog1.InitialDirectory = "C:\Documents and Settings\ADMIN\Desktop"
    openFileDialog1.Filter = ".csv files (*.csv)|*.csv"
    openFileDialog1.FilterIndex = 2
    openFileDialog1.RestoreDirectory = True

    If openFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        FileNameAndPath = openFileDialog1.FileName
        Dim fi As New IO.FileInfo(FileNameAndPath)
        DirectoryPath = fi.DirectoryName
        FileName = System.IO.Path.GetFileName(FileNameAndPath)

        'Note that the folder is specified in the connection string,
        'not the file. That's specified in the SELECT query, later.
        Dim connString As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" _
            & DirectoryPath & ";Extended Properties=""Text;HDR=No;FMT=Delimited"""
        Dim conn As New Odbc.OdbcConnection(connString)

        'Open a data adapter, specifying the file name to load
        Dim da As New Odbc.OdbcDataAdapter("SELECT * FROM [" & FileName & "]", conn)
        'Then fill a data table, which can be bound to a grid
        Dim dt As New DataTable
        da.Fill(dt)

        'grdCSVData.DataSource = dt
    End If
End Sub

我已阅读Reading CSV into using OLEDB并且我没有使用64位系统,cpu或visual studio。

How to read a CSV file into a .NET Datatable不起作用。我不想介绍任何自定义。这就是我更新这个项目的原因。

http://social.msdn.microsoft.com/Forums/en-US/4f860035-e081-44b9-a08c-b3911f682975/problem-using-odbcdatareader-to-read-from-a-csv-file说我应该只使用文件名,而不是SQL语句中的整个路径。这就是我在做的事。

我已经在某个地方Visual Basic How do I read a CSV file and display the values in a datagrid?

看到了这种形式或形式的代码

编辑:我可以通过鼠标在Dim da As New Odbc.OdbcDataAdapter行学习任何内容,例如连接失败或各个字段应该包含哪些信息?

1 个答案:

答案 0 :(得分:0)

我的问题是我的oledb适配器。为了便于阅读,我的按钮调用一个sub来打开openfiledialog,然后根据过滤器索引调用csv选项和xls选项。两者都非常相似,但xls需要检测工作表名称。我将包含以供参考。

Public Sub ConvertCSV(ByVal FileNameAndPath As String)
    Dim DirectoryPath, FileName As String
    Dim fi As New IO.FileInfo(FileNameAndPath)
    DirectoryPath = fi.DirectoryName
    FileName = System.IO.Path.GetFileName(FileNameAndPath)

    Dim inputString As String = ""

    Dim sr As New IO.StreamReader(FileNameAndPath)
    inputString = sr.ReadToEnd()
    sr.Close()

    Dim dtnew As New DataTable()
    Dim conn As New System.Data.OleDb.OleDbConnection()
    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DirectoryPath & ";Extended Properties=Text;"
    Dim da As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [" & FileName & "]", conn)
    da.Fill(dtnew)
End Sub

我的Excel解决方案:

Public Sub ConvertExcel(ByVal FileNameAndPath As String)

    Dim DirectoryPath, FileName As String
    Dim fi As New IO.FileInfo(FileNameAndPath)
    DirectoryPath = fi.DirectoryName
    FileName = System.IO.Path.GetFileName(FileNameAndPath)

    Dim inputString As String = ""

    Dim sr As New IO.StreamReader(FileNameAndPath)
    inputString = sr.ReadToEnd()
    sr.Close()

    Dim dtnew As New DataTable()
    Dim conn As New System.Data.OleDb.OleDbConnection()
    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FileNameAndPath & ";Extended Properties=Excel 8.0;"

    conn.Open()

    Dim dtSheets As DataTable = conn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, Nothing)
    Dim listSheet As New List(Of String)
    Dim drSheet As DataRow

    For Each drSheet In dtSheets.Rows
        listSheet.Add(drSheet("TABLE_NAME").ToString())
    Next

    ''Make a sheet selection Form
    'If dtSheets.Rows.Count > 1 Then

    'End If

    Dim StringCheck As String = "SELECT * FROM " & dtSheets.Rows(0).Item(2).ToString
    Dim da As New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [" & dtSheets.Rows(0).Item(2).ToString & "]", conn)
    da.Fill(dtnew)

End Sub

您只需要在其他地方声明一些表和适配器。

Dim da, ds, dt, dtnew As Object