Imports System.Data.SqlClient
Public Class SQLDatabase
Private mSQLDBConnString As String
Private mSQLDBCmd As SqlCommand
Private mSQLDBConnector As SqlConnection
Public Sub New(ByVal pConnStr As String)
mSQLDBConnString = pConnStr
Try
mSQLDBConnector = New SqlConnection(pConnStr)
mSQLDBConnector.Open()
Catch ex As Exception
Throw New Exception(ex.ToString())
End Try
End Sub
Public Property SQLDBConnString()
Get
Return mSQLDBConnString
End Get
Set(ByVal Value)
mSQLDBConnString = Value
End Set
End Property
Public Sub CloseSQLDB()
If IsSQLDBOpen() Then
mSQLDBConnector.Dispose()
mSQLDBConnector.Close()
mSQLDBConnector = Nothing
End If
End Sub
Public Sub OpenSQLDB()
If Not IsSQLDBOpen() Then
If mSQLDBConnector Is Nothing Then
'not even a valid database object
'try to recreate the object, object recreated if a string exists
If mSQLDBConnString.Length > 0 Then
mSQLDBConnector = New SqlConnection(mSQLDBConnString)
mSQLDBConnector.Open()
Else
'no string
Throw New Exception("Database null, please create a valid SQLDB object!")
Exit Sub
End If
Else
mSQLDBConnector.Open()
End If
End If
End Sub
Public Function IsSQLDBOpen() As Boolean
Return mSQLDBConnector.State = ConnectionState.Open
End Function
Public Function SQLDBUsed() As String
Return mSQLDBConnector.Database
End Function
Public Sub SetSQLDBCommand(ByVal pStrSprocName As String)
If mSQLDBConnector Is Nothing Then
mSQLDBConnector = New SqlConnection(mSQLDBConnString)
mSQLDBConnector.Open()
else
If Not IsSQLDBOpen() then
mSQLDBConnector.Open()
end if
End If
mSQLDBCmd = New SqlCommand(pStrSprocName, mSQLDBConnector)
With mSQLDBCmd
.CommandType = CommandType.StoredProcedure
End With
End Sub
Public Function UseSQLDBExecuteReader() As SqlDataReader
Dim lRead As SqlDataReader
If mSQLDBCmd Is Nothing Then
Throw New Exception("Must use SetSQLDBCommand to initialize SQLCommand object!")
Exit Function
End If
If mSQLDBConnector Is Nothing Then
mSQLDBConnector = New SqlConnection(mSQLDBConnString)
mSQLDBConnector.Open()
else
If Not IsSQLDBOpen() then
mSQLDBConnector.Open()
end if
End If
Try
With mSQLDBCmd
.Connection = mSQLDBConnector
lRead = mSQLDBCmd.ExecuteReader(CommandBehavior.CloseConnection)
End With
Return lRead
Catch ex As Exception
Throw New Exception(ex.ToString())
Finally
mSQLDBCmd.Dispose()
mSQLDBCmd = Nothing
mSQLDBConnector.Close()
mSQLDBConnector = Nothing
End Try
End Function
Public Function UseSQLDBDataSet() As DataSet
Dim lDs As DataSet
Dim lDataAdapter As SqlDataAdapter
If mSQLDBCmd Is Nothing Then
Throw New Exception("Must use SetDBCommand to initialize SQLCommand object!")
Exit Function
End If
If mSQLDBConnector Is Nothing Then
mSQLDBConnector = New SqlConnection(mSQLDBConnString)
mSQLDBConnector.Open()
Else
If Not IsSQLDBOpen() Then
mSQLDBConnector.Open()
End If
End If
Try
With mSQLDBCmd
lDataAdapter = New SqlDataAdapter
With lDataAdapter
.SelectCommand = mSQLDBCmd
End With
lDs = New DataSet
lDataAdapter.Fill(lDs)
End With
Return lDs
Catch ex As Exception
Throw New Exception(ex.ToString())
Finally
mSQLDBCmd.Dispose()
mSQLDBCmd = Nothing
lDs = Nothing
lDataAdapter = Nothing
mSQLDBConnector.Close()
mSQLDBConnector = Nothing
End Try
End Function
Public Sub UseSQLDBExecuteNonQuery()
If mSQLDBCmd Is Nothing Then
Throw New Exception("Must use SetSQLDBCommand to initialize SQLCommand object!")
Exit Sub
End If
If mSQLDBConnector Is Nothing Then
mSQLDBConnector = New SqlConnection(mSQLDBConnString)
mSQLDBConnector.Open()
Else
If Not IsSQLDBOpen() Then
mSQLDBConnector.Open()
End If
End If
Try
With mSQLDBCmd
.ExecuteNonQuery()
End With
Catch ex As Exception
Throw New Exception(ex.ToString())
Finally
mSQLDBCmd.Dispose()
mSQLDBCmd = Nothing
mSQLDBConnector.Close()
mSQLDBConnector = Nothing
End Try
End Sub
Public Function UseSQLDBExecuteScalar() As Integer
Dim intNumRows As Integer
If mSQLDBCmd Is Nothing Then
Throw New Exception("Must use SetSQLDBCommand to initialize SQLCommand object!")
Exit Function
End If
If mSQLDBConnector Is Nothing Then
mSQLDBConnector = New SqlConnection(mSQLDBConnString)
mSQLDBConnector.Open()
Else
If Not IsSQLDBOpen() Then
mSQLDBConnector.Open()
End If
End If
Try
With mSQLDBCmd
intNumRows = CInt(.ExecuteScalar().ToString())
End With
UseSQLDBExecuteScalar = intNumRows
Catch ex As Exception
Throw New Exception(ex.ToString())
Finally
mSQLDBCmd.Dispose()
mSQLDBCmd = Nothing
mSQLDBConnector.Close()
mSQLDBConnector = Nothing
End Try
End Function
Public Sub DeleteSQLDBCmd()
mSQLDBCmd.Dispose()
mSQLDBCmd = Nothing
End Sub
Public Sub AddSQLDBCmdParameter(ByVal pName As String, ByVal pType As SqlDbType, ByVal pVal As Object)
If mSQLDBCmd Is Nothing Then
Throw New Exception("Must use SetDBCommand to initialize SQLCommand object!")
Exit Sub
End If
If mSQLDBConnector Is Nothing Then
mSQLDBConnector = New SqlConnection(mSQLDBConnString)
mSQLDBConnector.Open()
Else
If Not IsSQLDBOpen() Then
mSQLDBConnector.Open()
End If
End If
Try
With mSQLDBCmd
.Parameters.Add(pName, pType).Value = pVal
End With
Catch ex As Exception
Throw New Exception(ex.ToString())
End Try
End Sub
Public Sub ClearSQLDBCmdParameters()
If mSQLDBCmd Is Nothing Then
Throw New Exception("Must use SetDBCommand to initialize SQLCommand object!")
Exit Sub
End If
With mSQLDBCmd
.Parameters.Clear()
End With
End Sub
End Class