Search code examples
c#exceloledb

oledb query-condition with column by number


I am reading a excel-document via oledb. When there is no headerrow, how do i use the columns in my query to set up conditions?

I want to do something like this:

SELECT * FROM [MY_SHEET$A3:M] WHERE [1] IS NOT NULL;

When i try this, i get the following error:

No value was specified for at least one required parameter.

I want to use the column-number for my condition. When i say HDR=TRUE in my connectionstring, i could use the column-name, but there are some duplicate-names in the document which leads to the problem, that i get the value of the wrong column. Renaming the columns in the document would be a solution, but is this possible with using the columnnumbers?


According the comments of Steve and Simo i tried the following:

SELECT * FROM [MY_SHEET$A3:M] WHERE [F1] IS NOT NULL;

But i get the same error-message. I tried it with HDR=TRUE and HDR=FALSE WIth [] arround F1 and wihtout.


Here i uploaded the xlsx-file: https://ufile.io/fky0l The exact command-text is SELECT * FROM [Sheet1$A3:M] WHERE F1 IS NOT NULL Here is the connectionstring

"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\stack.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=TRUE\""

Here is the codesnippet:

var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\stack.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=TRUE\"";

using (var connection = new OleDbConnection(connectionString)
{
    connection.Open();
    using (var command = connection.CreateCommand())
    {
        command.CommandText = $"SELECT * FROM [Sheet1$A3:M] WHERE F1 IS NOT NULL";
        var reader = command.ExecuteReader();
        while(reader.Read())
        {
            var value = reader.Get<string>(0);
            Console.WriteLine(value);
        }
    }
}

Solution

  • The probelm was my connectionstring. The possible values for HDR are YES and NO. I was using TRUE and FALSE. After changing my connectionstring to:

    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\stack.xlsx;Extended Properties=\"Excel 12.0 Xml;HDR=NO\""

    Everything worked...