无法通过OLE DB数据提供程序读取csv文件

时间:2011-06-09 18:18:48

标签: vb.net csv oledb

我无法使用OLEDB读取CSV文件。

我收到此错误:

  

...不是有效路径。确保路径名拼写正确,并且您已连接到文件所在的服务器

这是我的代码

'**********Genarate DataTable***************
Function CsvCreateDataTable(ByVal strPath As String, ByVal strFilesName As String) As DataTable
    Dim objConn As New OleDbConnection
    Dim dtAdapter As OleDbDataAdapter
    Dim dt As New DataTable

    'Response.Write(strPath);
    'm_objFile.SaveAs(Server.MapPath("~") + "\uploads\gallery\original\" + m_strFileName)
    Dim dirFilename As String
    'dirFilename = Server.MapPath("~") + strPath + strFilesName
    dirFilename = Server.MapPath("~\Uploads\") + "11backlist.csv"

    If File.Exists(dirFilename) Then

        Response.Write(dirFilename)

    Else
        Response.Write("ssssssss")
    End If

    Response.Write("<br><br><br>")
    Dim strConnString As String
    strConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & dirFilename & ";Extended Properties='TEXT;HDR=Yes;FMT=Delimited;Format=Delimited(,)'"

    objConn = New OleDbConnection(strConnString)
    objConn.Open()

    Dim strSQL As String
    strSQL = "SELECT * FROM " & strFilesName

    dtAdapter = New OleDbDataAdapter(strSQL, objConn)
    dtAdapter.Fill(dt)

    dtAdapter = Nothing

    objConn.Close()
    objConn = Nothing

    Return dt '*** Return DataTable ***'

End Function

0 个答案:

没有答案