OleDBConnection Connection string

2019-07-11 06:43发布

问题:

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();

回答1:

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



回答2:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + PathNam + ";
Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"

http://www.nullskull.com/q/10173180/hello-would-you-explain-imex-option-when-import-excel.aspx for IMEX usage



标签: c# oledb