我有這樣的錯誤有關DataReader仍處於打開狀態,但我發現,如果我使用一個不同的查詢/命令它工作正常DataReader的仍然是開放的,由於我的查詢
所以這個問題是我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
請出示在包含子的所有代碼這個錯誤。在這種情況下,圖像不是很有用。 – Steve
在這裏你走@Steve – kelvzy