试图在vb中读取这个csv文件,然后响应。在csv中写出数据

时间:2014-10-24 20:27:59

标签: vb.net csv dataset oledb oledbconnection

我在'/'应用程序中收到服务器错误。

无效的论点。

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.OleDb.OleDbException:参数无效。

当我尝试使用MyCommand.Fill阅读DataSet

这是我的代码

Dim sheets as new List(Of String)(New String(){"po"})

Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft Office 12.0 Access Database Engine OLE DB Provider;Data Source=" & filepath & ";Extended Properties=Excel 8.0;")   

for p as integer = 0 to sheets.count - 1
    dim dt as DataTable
    MyCommand = New System.Data.OleDb.OleDbDataAdapter("Select * from ["& sheets(p) & "$]", MyConnection)
    DtSet = New System.Data.DataSet
    MyCommand.Fill(DtSet)
    dt = DtSet.Tables(0)

    if p > 0
        response.write(sheets(p))
    end if

next
MyConnection.Close()

1 个答案:

答案 0 :(得分:0)

我有一些顾虑:

  • 您是否有理由使用 Microsoft Office 12.0 Access数据库引擎OLE DB提供程序?如何使用 Microsoft.Jet.OLEDB.4.0
  • 您确定您的数据库连接(即MyConnection)是 打开 吗?
  • 您也不希望用户收到未处理的错误。请在Try/Catch示例中使用this

首先尝试通过调用 MyConnection.Open()

打开连接
Dim sheets as new List(Of String)(New String(){"po"})

Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft Office 12.0 Access Database Engine OLE DB Provider;Data Source=" & filepath & ";Extended Properties=Excel 8.0;")  

MyConnection.Open() '<-------------------- open the connection here

for p as integer = 0 to sheets.count - 1
    dim dt as DataTable
    MyCommand = New System.Data.OleDb.OleDbDataAdapter("Select * from ["& sheets(p) & "$]", MyConnection)
    DtSet = New System.Data.DataSet
    MyCommand.Fill(DtSet)
    dt = DtSet.Tables(0)

    if p > 0
        response.write(sheets(p))
    end if

next
MyConnection.Close()