2011-10-27 43 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 
    } 
} 
相关问题