質問

I've created a small vb.net application that connects to a an excel sheet on a shared folder. It works great if i open the excel before using the the application , the issue is when the excel isn't open in the background i get a "System.Data.OleDb.OleDbException" error when trying to open a connection.

I've read a bit about this error and i understand it has something to do with access rights to a local temp library. So my questions are: 1) Is there a solution? 2) Is this the best connection strategy for my situation where the excel files sit on a shared drive?

Connection code:

    skuPath = "C:\path.xlsm"
    cn = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" + "data source=" + skuPath + ";Extended Properties=Excel 8.0;")
    q1 = "select * from [" + year + "$B4:V128]"
    da = New System.Data.OleDb.OleDbDataAdapter(q1, cn)
    cn.Open()
    da.Fill(ds, "Table1")
    cn.Close()
    dt = ds.Tables(0)

I receive the error on cn.Open().

役に立ちましたか?

解決

Got exactly the same problem few weeks ago.
Error is caused by incorrect version of oledb provider, just as Steve suggested.

Try this:

skuPath = "C:\path.xlsm"
cn = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;" + "data source=" + skuPath + ";Extended Properties=""Excel 12.0;HDR=Yes""")
q1 = "select * from [" + year + "$B4:V128]"
da = New System.Data.OleDb.OleDbDataAdapter(q1, cn)
cn.Open()
da.Fill(ds, "Table1")
cn.Close()
dt = ds.Tables(0)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top