2014-07-11 8 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은 당신이 게시 코드 외부에서 정의되어 폐기되지 않은, 그래서 여기에 몇 가지 가정을하고있다.

여기에 연결 풀링이 있다고 가정합니다. 즉, 이 작업을 실행할 때마다 새 db 연결을 얻으십시오. 두 번째 또는 후속 시간 동안이 코드를 누를 때 MySqlDataAdapter 등을 삭제하지 않으므로 DB와 함께 열린 '쿼리'에 여전히 있습니다. , 즉

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

을 그리고 사용 문에서도 변경 :

당신은 무엇에는 IDisposable를 확인하기 위해 필요

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

ok ok 시도해주세요. – kelvzy

+0

Try-Catch를 추가하고이 오류가 있습니다 ** 연결 (상태 열기) 중에 'ConnectionString'속성을 변경할 수 없습니다 ** – kelvzy

+0

지금도 여전히 동일합니다 오류 – kelvzy