【问题标题】:getoledbschematable requires an open and available connection.The connection's current state is closedgetoledbschematable 需要一个打开且可用的连接。连接的当前状态为关闭
【发布时间】:2018-11-02 12:40:15
【问题描述】:

当我输入标题时,下面的代码给了我以下错误

我在这里做错了什么?

OpenFileDialog openFileDialog1 = new OpenFileDialog();

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    string huru = openFileDialog1.FileName;
    this.textBox1.Text = huru;
    string pathConn;
    OleDbConnection conn;
    DataTable spreadSheetData;
    string sheetName = "";
    OleDbCommand onlineConnection;
    OleDbDataAdapter myDataAdapter;
    DataTable dt = new DataTable();

    if (huru.Substring(huru.Length - 3) == "lsx")
    {

        pathConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + huru
            + ";Extended Properties = \"Excel 12.0 Xml;HDR=YES\"; ";
    }
    else
    {
        pathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + huru
            + ";Extended Properties=\"Excel 8.0;HDR=yes;\";";
    }
    conn = new OleDbConnection(pathConn);
    spreadSheetData = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
    foreach (DataRow dr in spreadSheetData.Rows)
    {
        sheetName = dr["TABLE_NAME"].ToString();
        sheetName = sheetName.Replace("'", "");
        if (sheetName.EndsWith("$"))
        {
            onlineConnection = new OleDbCommand("SELECT * FROM [" + sheetName + "]", conn);
            myDataAdapter = new OleDbDataAdapter(onlineConnection);
            dt = new DataTable();
            dt.TableName = sheetName;
            myDataAdapter.Fill(dt);
            ds.Tables.Add(dt);
        }
    }
}

spreadSheetData 开始下降为空

我的代码参考Excel to DataGridView JohnG 回答的第一个问题

还有这个视频https://www.youtube.com/watch?v=CfNMPDJVjPI

感谢您的帮助!

【问题讨论】:

  • 你应该描述你想用你的代码做什么,将错误作为文本插入,而不是作为图像链接(有很多原因),并向我们展示你自己的一些调试工作

标签: c# excel import datagridview


【解决方案1】:

您的代码应该是这样的: 我已将您的连接封装在 using 中,因此我们肯定会释放资源。也需要打开连接。

 OpenFileDialog openFileDialog1 = new OpenFileDialog();
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            //Prepare things

            using(OleDbConnection conn = new OleDbConnection(pathConn))
            {
                conn.Open(); //Added this line
                spreadSheetData = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                foreach (DataRow dr in spreadSheetData.Rows)
                {
                      //Do staff
                }
            }
        }

【讨论】:

    【解决方案2】:

    您必须在使用前打开您的连接:

    conn = new OleDbConnection(pathConn);
    conn.Open();
    

    您还应该使用“Using”语句在不再使用时正确处理连接。

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string huru = openFileDialog1.FileName;
                    this.textBox1.Text = huru;
                    string pathConn;
                    //OleDbConnection conn;
                    DataTable spreadSheetData;
                    string sheetName = "";
                    OleDbCommand onlineConnection;
                    OleDbDataAdapter myDataAdapter;
                    DataTable dt = new DataTable();
    
                    if (huru.Substring(huru.Length - 3) == "lsx")
                    {
    
                        pathConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + huru
                            + ";Extended Properties = \"Excel 12.0 Xml;HDR=YES\"; ";
                    }
                    else
                    {
                        pathConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + huru
                            + ";Extended Properties=\"Excel 8.0;HDR=yes;\";";
                    }
    
                    using(OleDbConnection conn = ew OleDbConnection(pathConn))
                    {
                        //conn = new OleDbConnection(pathConn);
                        conn.Open();
                        spreadSheetData = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        foreach (DataRow dr in spreadSheetData.Rows)
                        {
                            sheetName = dr["TABLE_NAME"].ToString();
                            sheetName = sheetName.Replace("'", "");
                            if (sheetName.EndsWith("$"))
                            {
                                onlineConnection = new OleDbCommand("SELECT * FROM [" + sheetName + "]", conn);
                                myDataAdapter = new OleDbDataAdapter(onlineConnection);
                                dt = new DataTable();
                                dt.TableName = sheetName;
                                myDataAdapter.Fill(dt);
                                ds.Tables.Add(dt);
                            }
                        }
                    }
                }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 2015-04-17
    相关资源
    最近更新 更多