0

Rookie here... I need to get the column name from an excel file so I can use it as a string. I'm using C# with OLEDB.

private void CheckFiles();
{
    OleDbConnection MyConnection;
    DataSet DtSet;
    OleDbDataAdapter MyCommand;
    string file = @"C:\Users\...path...\2015.xlsm";
    MyConnection = new OleDbConnection(@"provider=Microsoft.ACE.OLEDB.12.0;Data Source='" +
                            file + "';Extended Properties=Excel 8.0;");
    MyCommand = new OleDbDataAdapter("Select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = " + 
                            tab, MyConnection);
    MyConnection.Open();
    MyCommand.TableMappings.Add("Table", "Net-informations.com");
    MyConnection.Close();
}

Thanks in advance!

4
  • sounds really good that you are using C# with OLEDB now show us what you have thus far so that we can help or someone else can lend you more advice. post your code showing us what you have tired... Commented Oct 6, 2015 at 2:23
  • stackoverflow.com/questions/3855101/… Commented Oct 6, 2015 at 2:25
  • @MethodMan I'm so new to this that I couldn't tell if you're being sarcastic... hehehe... anyway, I don't have much... I added the code I have so far to the original post. Thanks! Commented Oct 6, 2015 at 2:39
  • you need to use the Fill method to execute the OleDbAdapter sql command.. I will post an example on how I just tested excel on my own machine using JetProvider Commented Oct 6, 2015 at 2:49

1 Answer 1

0
string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;";

ConnectionString = string.Format(ConnectionString, @"FullPathToExcelFile"); //my path replace with your actual file path

OleDbConnection conn = new OleDbConnection(ConnectionString);
conn.Open();

OleDbCommand cmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", conn);
OleDbDataAdapter oleDBAdapter = new OleDbDataAdapter();
oleDBAdapter.SelectCommand = cmdSelect;

DataSet myDataset = new DataSet();
oleDBAdapter.Fill(myDataset);
conn.Close(); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.