【问题标题】:Error: The ConnectionString property has not been initialized错误:ConnectionString 属性尚未初始化
【发布时间】:2018-09-13 05:55:03
【问题描述】:
            string extension = Path.GetExtension(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 8.0;");

        using (System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection())
        {
            switch (extension)
            {
                case ".xls":
                    string xlsconStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 8.0;";
                    con.ConnectionString = xlsconStr;
                    break;

                case ".xlsx":
                case ".xlsm":
                    string xlsxconStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 12.0;";
                    con.ConnectionString = xlsxconStr;
                    break;
            }
                using (System.Data.OleDb.OleDbCommand oconn = new System.Data.OleDb.OleDbCommand("SELECT * FROM [" + listBox1.SelectedIndex.ToString() + "$]", con))
                {
                    // **There will be an error here**
                    // **The ConnectionString property has not been initialized.**
                    con.Open();

                    System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(oconn);
                    System.Data.DataTable data = new System.Data.DataTable();
                    adapter.Fill(data);
                    dataGridView1.DataSource = data;
                }
        }

代码有什么问题?我的目的是在 listBox 上的 selectedItem 上绘制所有数据库,每当我点击它时,它都会填充 dataGridView

顺便说一句,所选项目来自 MSExcel worksheetName

【问题讨论】:

  • 检查 extension 包含的内容。如果它不包含任何包含在switch 语句中的字符串,它不会在Open() 执行时设置任何连接字符串,并像你所说的那样抛出异常。
  • 我认为您只需要将文件路径传递给 Path.GetExtension 方法即可。
  • 我已经检查并更改了我的extension。如何执行并抛出异常?
  • extension 变量在您的previous question 中用于存储来自openFileDialog1 控件的扩展名,该控件将在switch 中使用以确定正确的连接字符串。不要修改它以包含连接字符串本身,因为switch 中的任何case 语句都不会触发,并且实际的连接字符串属性仍然为空。
  • 更新你的代码和错误信息

标签: c# excel


【解决方案1】:

您的代码需要一点点修复:

string extension = Path.GetExtension(openFileDialog1.FileName);

以前您的 GetExtension 试图在您的连接字符串上查找扩展名,该方法不可能,因为是连接字符串而不是路径!

所以你的固定代码应该是这样的:

    string extension = Path.GetExtension(openFileDialog1.FileName);

    using (System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection())
    {
        switch (extension)
        {
            case ".xls":
                string xlsconStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 8.0;";
                con.ConnectionString = xlsconStr;
                break;

            case ".xlsx":
            case ".xlsm":
                string xlsxconStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + openFileDialog1.FileName + "'; Extended Properties=Excel 12.0;";
                con.ConnectionString = xlsxconStr;
                break;
        }
            using (System.Data.OleDb.OleDbCommand oconn = new System.Data.OleDb.OleDbCommand("SELECT * FROM [" + listBox1.SelectedIndex.ToString() + "$]", con))
            {
                con.Open();

                System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(oconn);
                System.Data.DataTable data = new System.Data.DataTable();
                adapter.Fill(data);
                dataGridView1.DataSource = data;
            }
    }

现在您的代码将进入 switch case 语句以初始化您的连接字符串;)

【讨论】:

    猜你喜欢
    • 2013-10-21
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 2014-12-03
    相关资源
    最近更新 更多