Option Explicit On
Imports System.Windows.Forms
Imports System.Data.SqlClient
Public Class ServerConnection
#Region "Member Variables"
Private mstrUserID As String
Private mstrPassword As String
Shared adoConnection As SqlConnection
#End Region
#Region "Properties"
Public WriteOnly Property UserID() As String
Set(ByVal Value As String)
If Len(Value) > 0 Then
mstrUserID = Value
Else
MessageBox.Show("Invalid User ID", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
End If
End Set
End Property
Public WriteOnly Property Password() As String
Set(ByVal Value As String)
If Len(Value) > 0 Then
mstrPassword = Value
Else
MessageBox.Show("Invalid Password", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
End If
End Set
End Property
#End Region
Public Function ConnectToServer() As SqlConnection
If adoConnection Is Nothing Then
Dim strConnect As String
Dim strCatalog As String = "Solution"
strConnect = "Data Source = SQLSERVER; " & _
"Initial Catalog = " & strCatalog & "; " & _
"Persist Security Info = True; " & _
"User Id = " & mstrUserID & "; " & _
"Password = " & mstrPassword & "; " & _
"Packet Size = 4096"
adoConnection = New SqlConnection(strConnect)
Try
adoConnection.Open()
Catch
adoConnection = Nothing
MessageBox.Show("Either a bad userid or password")
Return adoConnection
End Try
MessageBox.Show("Connected")
End If
Return adoConnection
End Function
Public Sub Disconnect()
adoConnection = Nothing
MessageBox.Show("Disconnected")
End Sub
End Class