I am using Windows 10 Home (x64), when I run the application (x86 -> read .xlsx).
The application just show me
"The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine"
Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + xcFileInfo.ToString + ";Extended Properties=Excel 12.0;")
con.Open()
table = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim sheetname As String = table.Rows(0)("table_name").ToString
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter("select * from [" + sheetname + "]", con)
myDA.Fill(myTable)
End Using
Related
I have an excel file that has 3 sheets,
I was able to read those sheets into 3 different variables as datatable.
is there any way to run sql query on any datatable
please check the last line (my invention code)
here is my code:
Dim table_customers As DataTable
Dim table_materials As DataTable
Dim table_movement As DataTable
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.ACE.OLEDB.12.0;Data Source=" + Excel + ";Extended Properties=Excel 12.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [accounts$]", MyConnection)
DtSet = New System.Data.DataSet
MyCommand.Fill(DtSet)
table_customers = DtSet.Tables(0)
MyConnection.Close()
MsgBox("customers ok")
Dim sql = "select cusomer_name from '" & table_customers & "' where condition inner join '" & table_materials & "' on ..... "
I'm trying to create a temp table on my database from an Excel file uploaded by the user. I cannot understand where is the problem and why Visual Studio is throwing that exception.
Code
Private Sub Excel_Load(ByVal filePath As String)
Dim myConn As SqlConnection
Dim myCmd As SqlCommand
Dim sqlCmd As String
Dim filename As String = Path.GetFileNameWithoutExtension(filePath)
'Setting up Connection'
myConn = New SqlConnection("Server=*****;Database=*****;User ID=*****;Password=*****;Integrated Security=SSPI;")
myConn.Open()
'Create table'
sqlCmd = "CREATE TABLE XlsTemp AS (SELECT * FROM EXCELLINK [" & filename & "$])"
'Execute Query'
myCmd = myConn.CreateCommand()
myCmd.CommandText = sqlCmd
myCmd.ExecuteNonQuery()
myConn.Close()
End Sub
Exception
SqlException: The object name 'EXCELLINK' is not valid.
Peu_UNRAE is my Excel file.
At the end I come up with this solution:
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim fileExcelType As String
//Get the file extension in order to use the propper provider
If IO.Path.GetExtension(fileExcel.ToUpper) = ".XLS" Then
fileExcelType = "Excel 8.0"
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileExcel & "';Extended Properties=" & fileExcelType & ";")
Else
fileExcelType = "Excel 12.0"
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & fileExcel & "';Extended Properties=" & fileExcelType & ";")
End If
//Opening excel connection
MyConnection.Open()
Dim myTableName = MyConnection.GetSchema("Tables").Rows(0)("TABLE_NAME")
Dim MyCommand As OleDbDataAdapter = New OleDbDataAdapter(String.Format("SELECT * FROM [{0}] ", myTableName), MyConnection)
MyCommand.TableMappings.Add("Table", " ")
MyCommand.Fill(dt)
//Closing connection
MyConnection.Close()
Remarks:
I used // for the comments because the standar ' was giving some problem here on StackOverflow
I have the following problem, by using the connection string:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename +
";Extended Properties=Excel 12.0 Xml;"
Then, I can execute the open-task. Nevertheless, if I want to use the following connection string:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename +
";Extended Properties=Excel 12.0 Xml;HDR=NO"
I get a mistake, saying me that the installable isam could not be found.
What is wrong with the second connection string, as I need this one because in my worksheet no headers are used.
Thanks in advance
if you want to query excel you must have a '$' sign after the sheet name in order to define the sheet as a table.
anyway that is the code i wrote for that purpose:
Try
Dim Myconnetion As New OleDbConnection
Dim DataSet As System.Data.DataSet
Dim MyCOmmand As System.Data.OleDb.OleDbDataAdapter
Dim Path As String = fullpath
Myconnetion = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties=Excel 12.0;")
MyCOmmand = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM " & "[" & sheetname & "$]")
DataSet = New System.Data.DataSet
MyCOmmand.Fill(DataSet)
dgv.DataSource = DataSet.Tables(0)
Myconnetion.Close()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
I'm getting Server Error in '/' Application.
Invalid argument.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.OleDb.OleDbException: Invalid argument.
when I try to use MyCommand.Fill to read the DataSet
Here's my code
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()
I have a few concerns:
Is there a reason you are using Microsoft Office 12.0 Access Database Engine OLE DB Provider? How about using Microsoft.Jet.OLEDB.4.0?
Are you sure your DB connection (i.e. MyConnection) is open?
You also dont want your users getting unhandled errors. Look into using Try/Catch in this example.
First try to open the connection by calling 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()
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)