Pages

Sunday 11 August 2013

Global Declaration of Database Connection String Using Function


Adding Two Numbers must  need the following objects:
The code below is a function which declare as public method.

Public Function Opencon() As SqlConnection
        Dim conn As New SqlConnection(My.Settings.InventoryConn)
        conn.Open()
        Return conn
        conn.Close()

    End Function

Try to copy this code into your Module and you have already a global Declaration of  SqlConnection.

Note:
           The "InventoryConn" is the name of my Database Connection in Settings.

How to apply the function into your event Arguments?

     Try this code:

  Using conn = Opencon()
            'Your code here....
        End Using

The advantage to use this function is you don't need to declare the connect open and close whenever you use the database connection because it is already declare in your function.

No comments:

Post a Comment