[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

VB SQL Connection

In my attached code...

Do my  Dispose() lines belong properly where they are or after the "Finally"?

Do I need to dispose of the SqlDataAdaptor?
Dim da As SqlDataAdapter = New SqlDataAdapter(sqlCmd)
        Dim ds As DataSet = New DataSet
        Dim dt As DataTable = New DataTable

        Try
            sqlConn.Open()
            da.Fill(ds)
            For Each dt In ds.Tables
                dt.TableName = dt.Rows(0)("TableName").ToString()
            Next
            If ds.Tables.Count > 0 Then
                InjectScript.Text = ""
            Else
                InjectScript.Text = "No data returned on this deal"
            End If
        Catch ex As Exception
            InjectScript.Text = ex.ToString
        Finally
            sqlCmd.Dispose()
            sqlConn.Dispose()
        End Try

Open in new window

0
lrbrister
Asked:
lrbrister
  • 3
  • 2
  • 2
2 Solutions
 
sonawanekiranCommented:
They are at correct place. Finally block is always executed even though there is exception in try block. Its good practice to dispose object whenever you are done with that object
0
 
lrbristerAuthor Commented:
SHould I dispose of the data adaptor as well?
0
 
sonawanekiranCommented:
Yes. Dispose method of SqlDataAdapter is available because it uses important system resources.
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
Éric MoreauSenior .Net ConsultantCommented:
0
 
lrbristerAuthor Commented:
emoreau:
Since we're building an entire application on the fly...it's important that we catch exceptions which we're emailing to ourselves.

Does using catch exceptions?
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
lrbristerAuthor Commented:
Perfect.  Thanks folks
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now