1 Replies - 4352 Views - Last Post: 04 April 2014 - 03:48 AM

#1 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,291
  • Joined: 12-June 08

Basic SQL Command Setup

Post icon  Posted 20 March 2014 - 09:45 AM

I see have a hard time with the basic steps of sending data to the database (issuing commands like INSERT, DELETE, UPDATE). Here's my typical framework for when I need to run a non-query!

(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 cmd As SqlClient.SqlCommand
        Dim bReturn As Boolean = False

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

            cmd = New SqlClient.SqlCommand(sql, con) '-- 5.0 Set up the command object with the sql string and open connection.

            '--cmd.Parameters.AddWithValue("", "") '-- 6.0 If parameters are needed add them here.
            cmd.ExecuteNonQuery() '-- 7.0 Execute 
            bReturn = True '-- 8.0 Return true that the execution worked!
        Catch ex As Exception
            '-- 9.0 Deal with any exceptions here
            '-- MsgBox(ex.Message)
            bReturn = False
        Finally
            '-- 10.0 always close that connection!
            If con IsNot Nothing Then con.Dispose()
        End Try

        Return bReturn


Is This A Good Question/Topic? 0
  • +

Replies To: Basic SQL Command Setup

#2 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Basic SQL Command Setup

Posted 04 April 2014 - 03:48 AM

good one, but it would be very nice if it contains using Statement

This post has been edited by thava: 04 April 2014 - 03:49 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1