tags:

views:

36

answers:

2

Hello.
I'm trying to read from excel file with oleDB provider using C#:

                using (var fileConnection = new OleDbConnection(fileConnectionString))
                {
                    var command = new OleDbCommand(@"Select SourceName, [ExternalID] FROM [page1$]", fileConnection);
                    fileConnection.Open();
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        yield return new Source 
                        { 
//some code
                        };                
                    }
                }

. The only problem is that some headers in excel file have a square bracket in their name like [ExternalID]. Is it possible to read them? How can I do it?
Thank you for your help!

+2  A: 

Its simple. Create an excel doc which contains column called [ExternalId] and try get value. If you cant get that column value, use Adapter to get whole excel, and then you can find [ExternalID] column index. The index is what you need to get values.

Serkan Hekimoglu
+1  A: 

Have you tried putting the whole column name in quotes? I have seen some SQL clients accept quotes as well as square brackets.

e.g.

    using (var fileConnection = new OleDbConnection(fileConnectionString))
                {
                    var command = new OleDbCommand(@"Select \"SourceName\", \"[ExternalID]\" FROM [page1$]", fileConnection);
                    fileConnection.Open();
                    var reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        yield return new Source 
                        { 
//some code
                        };                
                    }
                }
Neil Fenwick