我有关于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
答案 0 :(得分:2)
MySqlDataAdapter是IDisposable,你没有处理它 MySQLcon和MySQLserver是在你发布的代码之外定义的,所以我在这里做了一些假设。
我认为这里有连接池,即每次运行时确实都不会获得新的数据库连接。因为当你在第二次或之后的时间点击这段代码时你没有处理你的MySqlDataAdapter等,你仍然可以打开&#39;查询&#39;与DB。
你需要检查所有的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
.....
并更改为使用陈述,即:
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)看起来毫无意义,你要在已经填充的数据集中添加一个空白表