Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Connection to SQL Server

Controls

Creating a Connection to SQL Server

by  SqueakinSweep  Posted    (Edited  )
As the first in a planned set of FAQ's Im including some Class code to allow users to create a connection to SQL Server. You can

1) Change the Constants to suit your own Server
2) Pass in a Connection String

--Usage

dim osql as new clsSQLDataConnection
dim bConnected as Boolean = osql.bConnect()
dim bDisconnected as Boolean = osql.bDisconnect()


Code:
Public Class clsSQLDataConnection

#Region " My Constants "

    Private Const sDefaultServer As String = "SQLSERVER"
    Private Const sDefaultDatabase As String = "Master"
    Private Const sDefaultUser As String = "sa"
    Private Const sDefaultPassword As String = ""
    Private Const iDefaultTimeOut As Integer = 10
    Private Const sDefaultPooling As String = "false"

#End Region

#Region " My Properties "

    Public Property oConnection() As SqlConnection
        Get
            Return varConnection
        End Get
        Set(ByVal Value As SqlConnection)
            varConnection = Value

        End Set
    End Property : Private varConnection As SqlConnection

    Private Property sConnectionString() As String
        Get
            Return varConnectionString
        End Get
        Set(ByVal Value As String)
            varConnectionString = Value
            Me.oConnection = New SqlConnection(Value)
        End Set
    End Property : Private varConnectionString As String

    Public ReadOnly Property oConnectionState() As System.Data.ConnectionState
        Get
            Return Me.oConnection.State
        End Get
    End Property

    Public ReadOnly Property oError() As Exception
        Get
            Return varError
        End Get
    End Property : Private varError As Exception

#End Region

#Region " My New "

    Public Sub New()
        Dim sb As New Text.StringBuilder

        sb.Append("Server=" & Me.sDefaultServer & ";")
        sb.Append("DataBase=" & Me.sDefaultDatabase & ";")
        sb.Append("User ID=" & Me.sDefaultUser & ";")
        sb.Append("Password=" & Me.sDefaultPassword & ";")
        sb.Append("Connect Timeout=" & Me.iDefaultTimeOut & ";")
        sb.Append("Pooling=" & Me.sDefaultPooling & ";")

        Me.sConnectionString = sb.ToString

    End Sub

    Public Sub New(ByVal sConnectionString As String)
        Me.sConnectionString = sConnectionString

    End Sub

    Public Sub New(ByVal sServer As String, ByVal sDatabase As String, ByVal sUser As String, ByVal sPassword As String, Optional ByVal iTimeout As Integer = iDefaultTimeOut)
        Dim sb As New Text.StringBuilder

        sb.Append("Server=" & sServer & ";")
        sb.Append("DataBase=" & sDatabase & ";")
        sb.Append("User ID=" & sUser & ";")
        sb.Append("Password=" & sPassword & ";")
        sb.Append("Connect Timeout=" & iTimeout & ";")
        sb.Append("Pooling=" & Me.sDefaultPooling & ";")

        Me.sConnectionString = sb.ToString

    End Sub

#End Region

#Region " My Methods "

    Public Function bConnect() As Boolean

        If Me.oConnectionState = ConnectionState.Open Then Return True

        Try
            Me.oConnection.Open()
            Return True

        Catch ex As Exception
            Me.varError = ex
            Return False

        End Try

    End Function

    Public Function bDisconnect() As Boolean

        If Me.oConnectionState = ConnectionState.Closed Then Return True

        Try
            Me.oConnection.Close()
            Return True

        Catch ex As Exception
            Me.varError = ex
            Return False

        End Try

    End Function

    Public Sub zDispose()
        Me.oConnection.Dispose()

    End Sub

#End Region

End Class
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top