0 Replies - 3570 Views - Last Post: 20 March 2014 - 09:45 AM

#1 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9573
  • View blog
  • Posts: 36,254
  • Joined: 12-June 08

Basic SQL Adapter Setup

Post icon  Posted 20 March 2014 - 09:45 AM

I see many folk struggling with the basic steps of returning data from a database. Here's my typical framework for when I need an adapter to get my data into a dataset.

(Typically this would go into its own function and you can tell how the process went by the 'bReturn' being true or false)

        
'-- 1.0 declare what you need
Dim sql As String = String.Empty
        Dim con As SqlClient.SqlConnection
        Dim adapt As SqlClient.SqlDataAdapter
        Dim ds As DataSet
        Dim bReturn As Boolean = False

        Try
            sql = "" '-- 2.0  SQL statement.
            con = New SqlClient.SqlConnection("") '-- AppSettings.Get("db") '-- 3.0 Connection string!
            con.Open()

            adapt = New SqlClient.SqlDataAdapter(sql, con) '-- 5.0 Set up the adapter object with the sql string and open connection.

            adapt.SelectCommand.Parameters.AddWithValue("", "")'-- 6.0 If parameters are needed add them here.
            ds = New DataSet '-- 7.0 Make sure the dataset is ready to catch the data!

            adapt.Fill(ds) '-- 8.0 fill the dataset object!

            bReturn = True'-- 9.0  Return true that the execution worked!
        Catch ex As Exception
 '-- 10.0 Deal with any exceptions here
            '-- MsgBox(ex.Message)
            bReturn = False
        Finally
 '-- 11.0 always close that connection!
            If con IsNot Nothing Then con.Dispose()
        End Try

        Return bReturn



Is This A Good Question/Topic? 0
  • +

Page 1 of 1