c#ExecuteNonQuery需要一个开放且可用的连接。连接的当前状态已关闭。

时间:2011-10-27 14:19:06

标签: c# excel

有人可以找到我收到此错误的原因吗?我已将其标记为我收到错误

public string ExportRecords(string query, string sheetname)
    {
        string filename = "";
        DataSet ds = new DataSet("New_DataSet");
        DataTable dt = new DataTable(sheetname);

        ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
        dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["KMFConnectionString"].ToString());
        con.Open();

        string sql = query;
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataAdapter adptr = new SqlDataAdapter();

        adptr.SelectCommand = cmd;
        adptr.Fill(dt);
        con.Close();

        ds.Tables.Add(dt);

        string connstr = connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + orgrepository.OrganizationMetaValueByKey("KMFFileDownloadPath") + filename + "; Extended Properties=Excel 8.0";


        OleDbConnection connection = new OleDbConnection(connstr);


        using (OleDbCommand commands = connection.CreateCommand())
        {
            commands.CommandText = "CREATE TABLE [Sheet20] (F1 number, F2 char(255), F3 char(128))";
            commands.ExecuteNonQuery();     ****getting error here****
            for (int i = 1; i <= 20; i++)
            {

                commands.CommandText = "INSERT INTO [Sheet20] (F1, F2, F3) VALUES(1,\"Fake Record\",\"Fake Record\")";
                commands.ExecuteNonQuery();
            }
        }

        if (dt.Rows.Count > 0)
        {
            //filename = sheetname + DateTime.Today.Day.ToString() + DateTime.Today.Month.ToString() + DateTime.Today.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".xlsx";
            filename = sheetname + "-output" + ".xls";                
            ExcelLibrary.DataSetHelper.CreateWorkbook(orgrepository.OrganizationMetaValueByKey("KMFFileDownloadPath") + filename, ds);


            //    connection.Close();

       }
        return filename;
    }

3 个答案:

答案 0 :(得分:2)

您需要像这样打开和关闭连接

 using (OleDbConnection connection = new OleDbConnection(connstr))
 {
     connection.Open();
     using (OleDbCommand commands = connection.CreateCommand())
     {
         //snip
     }
 }

答案 1 :(得分:0)

您的连接已关闭,请尝试打开命令并处理错误(如果有)。

using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        // The insertSQL string contains a SQL statement that
        // inserts a new row in the source table.
        OleDbCommand command = new OleDbCommand(insertSQL);

        // Set the Connection to the new OleDbConnection.
        command.Connection = connection;

        // Open the connection and execute the insert command.
        try
        {
            connection.Open();
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the
        // code exits the using block.
    }

答案 2 :(得分:0)

您尚未调用connection.Open()来打开您尝试使用的连接。