2011-10-27 46 views
0

可以有人發現,爲什麼我得到這個錯誤?我已標記它在哪裏我得到錯誤c#ExecuteNonQuery需要一個開放且可用的Connection。連接的當前狀態已關閉。

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

你在哪裏打開w.r.t OleDbConnection的連接的連接? – Zenwalker

+0

'string connstr = connstr = ...'只需要'string connstr = ...' – Ray

+0

您打開了您的SQLConnection,但不是您的OleDbConnection。另外,在'使用'塊中包裝... –

回答

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

您還沒有撥打connection.Open()來打開您嘗試使用的連接。

2

你需要打開和關閉,像這樣

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