OleDBConnection连接字符串

时间:2014-01-02 00:38:55

标签: c# oledb

当我尝试此代码OleDBConnection.open()无效并且没有抛出任何错误时,只需打开Windows窗体并说出任何我看到的消息框try1但是程序没有显示try2我的连接字符串中有什么问题请帮助我也试过excel 12.0,但它在参考文献(Microsoft Excel 14.0对象库)中参考Excel 14.0,该文件存在于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();

2 个答案:

答案 0 :(得分:3)

试试这个

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

“HDR =是;”表示第一行包含列名,而不是数据。 “HDR =无;”表明相反。

reference

答案 1 :(得分:1)

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 用于IMEX使用