pixabay_vb

DataBaseManagementMssql (VB.NET)

Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.IO
Imports GRI.CryptManagement.Contracts
Imports GRI.DataBaseManagementMssql2008.Contracts
Imports Ninject

Namespace GRI.DataBaseManagementMssql2008

Public Class DataBaseManagerMssql2008
Inherits IDataBaseManagerMssql2008

Private ReadOnly _cryptManager As ICryptManager

<Inject>
Public Sub New(ByVal cryptManager As ICryptManager)
_cryptManager = cryptManager
End Sub

Public Function GetDataTable(ByVal connectionString As String, ByVal queryString As String, ByVal Optional parameterList As List(Of SqlParameter) = Nothing, ByVal Optional isEncryptedConnectionString As Boolean = False) As DataTable
Validate(connectionString, queryString)
If isEncryptedConnectionString Then
Try
connectionString = _cryptManager.Decrypt(connectionString)
Catch cryptException As Exception
Throw New DataBaseManagerExceptionMssql2008(„connectionString cannot be decrypted“, cryptException)
End Try
End If

Dim dtRet = New DataTable()
Try
Using con = New SqlConnection(connectionString)
Using adp = New SqlDataAdapter(queryString, con)
adp.SelectCommand.CommandTimeout = 120
If parameterList IsNot Nothing Then
For Each param In parameterList
Try
adp.SelectCommand.Parameters.Add(param)
Catch e As Exception
Throw New DataBaseManagerExceptionMssql2008(„Parameters cannot be added to the dataAdapter“, e)
End Try
Next
End If

Try
adp.Fill(dtRet)
Catch e As Exception
Throw New DataBaseManagerExceptionMssql2008(„Recordset cannot be written to the dataAdapter“, e)
End Try
End Using
End Using

Return dtRet
Catch e As Exception
Throw New DataBaseManagerExceptionMssql2008(„An unforseen error has ocurred“, e)
End Try
End Function

Public Sub ExecuteNonQuery(ByVal connectionString As String, ByVal queryString As String, ByVal Optional parameterList As List(Of SqlParameter) = Nothing, ByVal Optional isEncryptedConnectionString As Boolean = False)
Validate(connectionString, queryString)
If isEncryptedConnectionString Then
Try
connectionString = _cryptManager.Decrypt(connectionString)
Catch cryptException As Exception
Throw New DataBaseManagerExceptionMssql2008(„connectionString cannot be decrypted“, cryptException)
End Try
End If

Using connection = New SqlConnection(connectionString)
Using command = New SqlCommand(queryString, connection)
If parameterList IsNot Nothing Then
For Each param In parameterList
Try
command.Parameters.Add(param)
Catch e As Exception
Throw New DataBaseManagerExceptionMssql2008(„Paramters cannot be added to command object“, e)
End Try
Next
End If

Try
connection.Open()
command.ExecuteNonQuery()
Catch e As Exception
Throw New DataBaseManagerExceptionMssql2008(„Command cannot be executed“, e)
End Try
End Using
End Using
End Sub

Private Shared Sub Validate(ByVal connectionString As String, ByVal queryString As String)
CheckValidConnectionString(connectionString)
CheckValidQueryString(queryString)
End Sub

Private Shared Sub CheckValidQueryString(ByVal queryString As String)
If queryString Is Nothing Then Throw New DataBaseManagerExceptionMssql2008(„QueryString cannot be null“, New ArgumentNullException(„queryString“))
If queryString = String.Empty Then Throw New DataBaseManagerExceptionMssql2008(„QueryString cannot contain only empty characters“, New ArgumentException(„QueryString cannot contain only empty characters“))
End Sub

Private Shared Sub CheckValidConnectionString(ByVal connectionString As String)
If connectionString Is Nothing Then
Throw New DataBaseManagerExceptionMssql2008(„“, New ArgumentNullException(„connectionString“))
End If

If connectionString = String.Empty Then
Throw New DataBaseManagerExceptionMssql2008(„“, New ArgumentException(„connectionString cannot contain only empty characters“))
End If
End Sub
End Class
End Namespace

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*