0
votes

I keep on getting this error when trying to use OleDb, and can't figure out how to solve the issue. I've tried referencing other websites and whatnot but got nothing. Here is what my code looks like:

OleDbCommand command = new OleDbCommand(@"SELECT [Name], [Config status], [OverallStatus], [NumHosts], [numEffectiveHosts], [TotalCpu], [NumCpuCores], [NumCpuThreads], [EffectiveCpu], [TotalMemory], [EffectiveMemory], [Num VMotions], [HA enabled], [Failover Level], [AdmissionControlEnabled], [Host monitoring], [HB Datastore Candidate Policy], [Isolation Response], [Restart Priority], [Cluster Settings], [Max Failures], [Max Failure Window], [Failure Interval], [Min Up Time], [VM Monitoring], [DRS enabled], [DRS default VM behavior], [DRS vmotion rate], [DPM enabled], [DPM default behavior], [DPM Host Power Action Rate] FROM [" + Sheet + "$]", conn); //Selects everything inside excel file
        DataTable Data = new DataTable();
        OleDbDataAdapter adapter = new OleDbDataAdapter(command);
        adapter.Fill(Data); //Puts all data inside a DataSet

The error occurs on the adapter.Fill(Data) line. Thanks for any help you may bring!

Edit: For further details, I am trying to pull data out of an Excel spreadsheet using OleDb, put that data into a DataTable object, and then use entity framework to push that data to a SQL server database. In my excel spreadsheet, the sheet I'm trying to access is named "vCluster". Could the issues lie in my OleDbConnection string?

1
Do you have a header row in your spreadsheet? What is your connectionstring? Double check the names of your columns against the name of the excel columns. If you have a typo then this error appears.Steve
I do have a header row in the spreadsheet, and I just went through and checked the names of the columns against the names of the excel columns. I found one minor spacing issue, but after fixing that I still got the same error. What do you mean by "If you have a type then this error appears"?CKneeland
It was a typo and not it is corrected to mean exactly "typo". However. If there is a mismatch between the column names (in excel) and the column names in C# OleDb thinks that you are supplying a parameter and raises the error.Steve
Check also the table name. It should match the name of the sheet in excel.Steve
Thanks for the help Steve! It was in fact an error with the column names in Excel compared to the ones in C#.CKneeland

1 Answers

0
votes

You need to remove $ from the query.

@"SELECT [Name], [Config status], [OverallStatus], [NumHosts], [numEffectiveHosts], [TotalCpu], [NumCpuCores], [NumCpuThreads], [EffectiveCpu], [TotalMemory], [EffectiveMemory], [Num VMotions], [HA enabled], [Failover Level], [AdmissionControlEnabled], [Host monitoring], [HB Datastore Candidate Policy], [Isolation Response], [Restart Priority], [Cluster Settings], [Max Failures], [Max Failure Window], [Failure Interval], [Min Up Time], [VM Monitoring], [DRS enabled], [DRS default VM behavior], [DRS vmotion rate], [DPM enabled], [DPM default behavior], [DPM Host Power Action Rate] FROM [" + temp + "]";