OleDBConnection Connection string

岁酱吖の 提交于 2019-12-11 02:46:21

问题


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



来源:https://stackoverflow.com/questions/20874795/oledbconnection-connection-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!