2014-07-11 49 views
0

我有這樣的錯誤有關DataReader仍處於打開狀態,但我發現,如果我使用一個不同的查詢/命令它工作正常DataReader的仍然是開放的,由於我的查詢

enter image description here

所以這個問題是我mycommand的字符串,但這是我需要加載所需的數據,所以任何建議如何解決它?由於

 Private Sub daily() 
    Cursor = Cursors.WaitCursor() 
     MySQLcon.ConnectionString = MySQLserver 
     MySQLcon.Open() 
     'strPath & "\" & txtName.Text & ".xlsx" 
     Dim dte As String = cboDate1.Text 
     Dim myCommand1 As New MySql.Data.MySqlClient.MySqlCommand 
     Dim myadapter As New MySql.Data.MySqlClient.MySqlDataAdapter 
     Dim mybuilder As New MySql.Data.MySqlClient.MySqlCommandBuilder 
     Dim mydatatable As New DataTable 
     Dim mydataset As New DataSet 
     myCommand1.Connection = MySQLcon 

     'myCommand1.CommandText = "SELECT `" + dte + "`.`Users`,concat((`users`.`Last Name`),', ',(`users`.`First Name`)) as `Name`,'" + dte + "' as Date,`" + dte + "`.`Activity`,`" + dte + "`.`Field1` as `Project Name`,`" + dte + "`.`Field2` as `Job Name` ," + _ 
     ' "SUM(`" + dte + "`.`Field4`) as `Records`, SUM(`" + dte + "`.`Field5`)as Pages," + _ 
     ' "ROUND(sum(TIME_TO_SEC(`" + dte + "`.`Elapsed Time`))/3600,2)as `Hours`," + _ 
     ' "'G' as `REGS/HR`," + _ 
     ' "'H' as `PGS/HR`" + _ 
     ' "FROM `" + dte + "`" + _ 
     ' "INNER JOIN `users`" + _ 
     ' "ON `" + dte + "`.`Users`=`users`.`Employee ID`" + _ 
     ' "Where `" + dte + "`.`Group` = 'DATA ENTRY' and `" + dte + "`.`Field1` <> 'OVER-BREAK 1' and `" + dte + "`.`Field1` <> 'OVER-BREAK 2' and (`" + dte + "`.`Activity` = 'KE' or `" + dte + "`.`Activity` = 'CH' or `" + dte + "`.`Activity` = 'SJ' or `" + dte + "`.`Activity` = 'VE' or `" + dte + "`.`Activity` = 'MB' or `" + dte + "`.`Activity` = 'TD' or `" + dte + "`.`Activity` = 'LK' or `" + dte + "`.`Activity` = 'PG')" + _ 
     ' "GROUP BY `" + dte + "`.`Users`, `" + dte + "`.`Activity`, `" + dte + "`.`Field1`" 

     myCommand1.CommandText = "SELECT `MERGED DATAENTRY`.`Users`,concat((`users`.`Last Name`),', ',(`users`.`First Name`)) as `Name`,'" + cboDate1.Text & " - " & cboDate2.Text + "' as Date,`MERGED DATAENTRY`.`Activity`,`MERGED DATAENTRY`.`Field1` as `Project Name`,`MERGED DATAENTRY`.`Field2` as `Job Name` ," + _ 
      "SUM(`MERGED DATAENTRY`.`Field4`) as `Records`, SUM(`MERGED DATAENTRY`.`Field5`)as Pages," + _ 
      "ROUND(sum(TIME_TO_SEC(`MERGED DATAENTRY`.`Elapsed Time`))/3600,2)as `Hours`," + _ 
      "'G' as `REGS/HR`," + _ 
      "'H' as `PGS/HR`" + _ 
      "FROM `MERGED DATAENTRY`" + _ 
      "INNER JOIN `users`" + _ 
      "ON `MERGED DATAENTRY`.`Users`=`users`.`Employee ID`" + _ 
      "Where `MERGED DATAENTRY`.`Group` = 'DATA ENTRY' and `MERGED DATAENTRY`.`Field1` <> 'OVER-BREAK 1' and `MERGED DATAENTRY`.`Field1` <> 'OVER-BREAK 2' and (`MERGED DATAENTRY`.`Activity` = 'KE' or `MERGED DATAENTRY`.`Activity` = 'CH' or `MERGED DATAENTRY`.`Activity` = 'SJ' or `MERGED DATAENTRY`.`Activity` = 'VE' or `MERGED DATAENTRY`.`Activity` = 'MB' or `MERGED DATAENTRY`.`Activity` = 'TD' or `MERGED DATAENTRY`.`Activity` = 'LK' or `MERGED DATAENTRY`.`Activity` = 'PG')" + _ 
      "GROUP BY `MERGED DATAENTRY`.`Users`, `MERGED DATAENTRY`.`Activity`, `MERGED DATAENTRY`.`Field1`" 
     myadapter.SelectCommand = myCommand1 
     myadapter.Fill(mydataset) 
     mydataset.Tables.Add(mydatatable) 
     DataGridView1.DataSource = mydataset.Tables(0) 

     Dim xlApp As Microsoft.Office.Interop.Excel.Application 
     Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook 
     Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet 
     Dim misValue As Object = System.Reflection.Missing.Value 


     Dim i As Integer 
     Dim j As Integer 

     xlApp = New Microsoft.Office.Interop.Excel.ApplicationClass 
     xlWorkBook = xlApp.Workbooks.Add(misValue) 
     xlWorkSheet = xlWorkBook.Sheets("sheet1") 


     For i = 0 To DataGridView1.RowCount - 1 
      For j = 0 To DataGridView1.ColumnCount - 1 
       For k As Integer = 1 To DataGridView1.Columns.Count 
        'Dim style As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("NewStyle") 
        'style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow) 
        'xlWorkSheet.Cells(1, k).Style = "NewStyle" 
        xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText 
        xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString() 
        If DataGridView1(j, i).ColumnIndex = 9 Then 
         xlWorkSheet.Cells(i + 2, j + 1) = "=ROUND(" & DataGridView1(j, i).Value.ToString() & (DataGridView1(j, i).RowIndex + 2) & "/" & "I" & (DataGridView1(j, i).RowIndex + 2) & ",2)" 

        End If 
        If DataGridView1(j, i).ColumnIndex = 10 Then 
         xlWorkSheet.Cells(i + 2, j + 1) = "=ROUND(" & DataGridView1(j, i).Value.ToString() & (DataGridView1(j, i).RowIndex + 2) & "/" & "I" & (DataGridView1(j, i).RowIndex + 2) & ",2)" 

        End If 
       Next 
      Next 
     Next 

     xlWorkSheet.SaveAs(strPath & "\" & txtName.Text & ".xlsx") 
     xlWorkBook.Close() 
     xlApp.Quit() 
     ReleaseObject(xlApp) 
     ReleaseObject(xlWorkBook) 
     ReleaseObject(xlWorkSheet) 
     MessageBox.Show("File Export Successfully!") 
    MySQLcon.Close() 

End Sub 
+0

請出示在包含子的所有代碼這個錯誤。在這種情況下,圖像不是很有用。 – Steve

+0

在這裏你走@Steve – kelvzy

回答

2

MySQLDataAdapter用於IDisposable的,你是不是處置它 MySQLcon和MySQLserver你發佈的代碼之外定義的,所以我在這裏做一些假設。

我會想象有連接池在這裏,即你不真的每次你運行這個時得到一個新的數據庫連接。由於您在第二次或以後的時間點擊此代碼時不會丟棄MySqlDataAdapter等內容,因此您仍然需要使用數據庫打開「查詢」。

您需要檢查IDisposable接口上的所有的:

Dim myCommand1 As New MySql.Data.MySqlClient.MySqlCommand 
Dim myadapter As New MySql.Data.MySqlClient.MySqlDataAdapter 
Dim mybuilder As New MySql.Data.MySqlClient.MySqlCommandBuilder 
..... 

,並切換到使用statments,即:

Using myadapter As New MySql.Data.MySqlClient.MySqlDataAdapter 
    Using myCommand1 As New MySql.Data.MySqlClient.MySqlCommand 
     Using mybuilder As New MySql.Data.MySqlClient.MySqlCommandBuilder 
     ..... 
     End Using 
    End Using 
End Using 

雖然我懷疑DataAdaptor是唯一一個真正重要的。

嘗試在IDisposable接口讀了

NB mydataset.Tables.Add,要添加一個空白表到一個已經裝滿數據集(mydatatable)看起來毫無意義的

+0

好吧好吧讓我試試這個.. – kelvzy

+0

我添加Try-Catch並得到這個錯誤**不允許在連接(狀態打開)時更改'ConnectionString'屬性** – kelvzy

+0

現在仍然是相同的錯誤 – kelvzy

相關問題