【问题标题】:c# ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.c# ExecuteNonQuery 需要一个打开且可用的连接。连接的当前状态为关闭。
【发布时间】:2011-10-27 14:19:06
【问题描述】:

有人能找到我为什么会收到此错误吗?我已经在我收到错误的地方标记了它

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;
    }

【问题讨论】:

  • 你在哪里打开了 w.r.t OleDBConnection 的连接?
  • string connstr = connstr = ... 只需string connstr = ...
  • 您打开了 SQLConnection,但没有打开 OleDbConnection。另外,将东西包装在using 块中......

标签: c# excel


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      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.
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-26
        • 2011-04-02
        • 2015-04-17
        • 1970-01-01
        • 1970-01-01
        • 2022-08-03
        • 1970-01-01
        相关资源
        最近更新 更多