INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Controls

Creating a Connection to SQL Server by SqueakinSweep
Posted: 20 Feb 04

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()



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


Back to Visual Basic (Microsoft) VB.NET FAQ Index
Back to Visual Basic (Microsoft) VB.NET Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close