由于我的查询,DataReader 仍然打开
Posted
技术标签:
【中文标题】由于我的查询,DataReader 仍然打开【英文标题】:DataReader is still open due to my Query 【发布时间】:2014-07-11 08:26:38 【问题描述】:我有这个关于 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
【问题讨论】:
请显示包含此错误的子代码中的所有代码。在这种情况下,图像并没有真正的用处。 【参考方案1】: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
.....
并改为使用语句,即:
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) 看起来毫无意义,您正在向已填充的数据集添加一个空白表
【讨论】:
我添加 Try-Catch 并收到此错误 在连接时不允许更改 'ConnectionString' 属性(状态打开) 我发现这是我的问题 myCommand1.Connection = MySQLcon以上是关于由于我的查询,DataReader 仍然打开的主要内容,如果未能解决你的问题,请参考以下文章
错误:已经有一个与此命令关联的打开DataReader,必须先关闭它。多个用户
Azure 表查询:System.InvalidOperationException:“已经有一个打开的 DataReader 与此命令关联,必须先关闭。”