Search code examples
c#oledb

OleDBConnection Connection string


When I try this code OleDBConnection.open() not work and didn't throw any error, just open windows form and say anything I see messageBox try1 but program didn't show try2 what is the wrong in my connection string please help I've tried also excel 12.0 but it looks in reference Excel 14.0 in References (Microsoft Excel 14.0 Object Library) and the file is exist in c:\product.xlsx

OleDbConnection conn_exel = new OleDbConnection(@"provider=Microsoft.Jet.OLEDB.12.0; Data Source=C:\product.xlsx; Extended Properties=""Excel 14.0;HDR=Yes;""");
        conn_exel.Open();
        MessageBox.Show("try2");
        OleDbCommand command_exel = new OleDbCommand(@"SELECT * FROM [Sayfa1$] WHERE id = 1",conn_exel);
        OleDbDataReader reader_exel =  command_exel.ExecuteReader();
        MessageBox.Show("try3");
        while (reader_exel.Read())
        {
            MessageBox.Show(reader_exel.GetString(1));
        }
        conn_exel.Close();

Solution

  • Try like this

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
    Extended Properties="Excel 12.0 Xml;HDR=YES";
    

    "HDR=Yes;" indicates that the first row contains columnnames, not data. "HDR=No;" indicates the opposite.

    reference