I have the following code that accesses a SQL-Server datsbase using the Data Access Block. My question is, do I need to dispose of the Database, SQLCmd, and Datatable object (like in a TRY FINALLY block) or is this taken care of automatically?
Private Sub Load_dvInjuryAgent()
Dim db As Database = Nothing, sqlCmd As DbCommand = Nothing, dt As New DataTable
Try
db = DatabaseFactory.CreateDatabase("InjuryTrackRead")
sqlCmd = db.GetStoredProcCommand("InjuryAgentSelect")
db.AddInParameter(sqlCmd, "InjuryAgentCode", DbType.Int16, Me.ddlInjuryAgent.SelectedValue)
dt.Load(db.ExecuteReader(sqlCmd))
Me.dvInjuryAgent.DataSource = dt
Me.dvInjuryAgent.DataBind()
Catch ex As Exception
sf.WriteErrorLog("LoaddvInjuryAgent", Err.Number, ex.Message, FormatSupportingInfo() & ex.ToString, sqlCmd)
sf.RedirectToStandardError()
End Try
End Sub
rspiet
Member
23 Points
122 Posts
Disposing of Datbase and SQLCmd
Feb 01, 2013 06:55 PM|LINK
I have the following code that accesses a SQL-Server datsbase using the Data Access Block. My question is, do I need to dispose of the Database, SQLCmd, and Datatable object (like in a TRY FINALLY block) or is this taken care of automatically?
Private Sub Load_dvInjuryAgent() Dim db As Database = Nothing, sqlCmd As DbCommand = Nothing, dt As New DataTable Try db = DatabaseFactory.CreateDatabase("InjuryTrackRead") sqlCmd = db.GetStoredProcCommand("InjuryAgentSelect") db.AddInParameter(sqlCmd, "InjuryAgentCode", DbType.Int16, Me.ddlInjuryAgent.SelectedValue) dt.Load(db.ExecuteReader(sqlCmd)) Me.dvInjuryAgent.DataSource = dt Me.dvInjuryAgent.DataBind() Catch ex As Exception sf.WriteErrorLog("LoaddvInjuryAgent", Err.Number, ex.Message, FormatSupportingInfo() & ex.ToString, sqlCmd) sf.RedirectToStandardError() End Try End Subsantosh.jagd...
Star
7625 Points
1454 Posts
Re: Disposing of Datbase and SQLCmd
Feb 05, 2013 07:34 AM|LINK
You should dispose non usable objects to release allocated memory by that objects.
MCP