【发布时间】:2013-01-20 21:00:00
【问题描述】:
例如在表单加载事件中:
Dim Conn As New OleDbConnection(ConnectionString)
Dim dataAdapter As New OleDb.OleDbDataAdapter
Dim dt As New Datatable
Dim Command As New OleDbCommand
Try
Command.CommandText = "select agentName from agents order by agentName"
dataAdapter = New OleDb.OleDbDataAdapter(Command.CommandText, Conn)
dataAdapter.Fill(dt)
agentsV.DataSource = dt
agentsV.ValueMember = "agentName"
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation + MsgBoxStyle.MsgBoxRight)
Finally
Conn.Dispose()
dataAdapter.Dispose()
Command.Dispose()
End Try
有一个对象没有被释放,它是dt datatable,所以如果make dispose,代理的comboBox数据源将被清除!
一般情况下如何处理?
谢谢。
【问题讨论】:
-
您不必处理数据表。检查此以获取更多详细信息:stackoverflow.com/questions/913228/…
-
如果我理解您的问题,创建并分配一个 FormClosing 事件处理程序。在事件处理程序中,添加 myDataTable.Dispose()。
-
谢谢你,在你的链接中,作者说“system.data 命名空间没有非托管资源”,所以不需要处理!任何机构都可以对此说更多吗?