163 lines
5.9 KiB
VB.net
163 lines
5.9 KiB
VB.net
Imports System.Data.SqlClient
|
|
Imports DigitalData.Modules.Logging
|
|
|
|
Public Class MSSQLServer
|
|
Public DBInitialized As Boolean = False
|
|
Public CurrentSQLConnectionString As String = ""
|
|
Private _Logger As Logger
|
|
|
|
Public Sub New(LogConfig As LogConfig, ConnectionString As String)
|
|
_Logger = LogConfig.GetLogger()
|
|
|
|
CurrentSQLConnectionString = ConnectionString
|
|
|
|
Try
|
|
DBInitialized = TestCanConnect()
|
|
Catch ex As Exception
|
|
DBInitialized = False
|
|
_Logger.Error(ex)
|
|
End Try
|
|
End Sub
|
|
|
|
Private Function TestCanConnect() As Boolean
|
|
Try
|
|
Dim oConnection As New SqlConnection(CurrentSQLConnectionString)
|
|
oConnection.Open()
|
|
oConnection.Close()
|
|
Return True
|
|
Catch ex As Exception
|
|
_Logger.Error(ex)
|
|
Return False
|
|
End Try
|
|
End Function
|
|
|
|
Private Function GetSQLConnection() As SqlConnection
|
|
Try
|
|
Dim oConnection As New SqlConnection(CurrentSQLConnectionString)
|
|
oConnection.Open()
|
|
Return oConnection
|
|
Catch ex As Exception
|
|
_Logger.Error(ex)
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
''' <summary>
|
|
''' Returns a datatable for a sql-statement
|
|
''' </summary>
|
|
''' <param name="sqlcommand">sqlcommand for datatable (select XYZ from TableORView)</param>
|
|
''' <param name="Timeout">Optional Timeout</param>
|
|
''' <returns>Returns a datatable</returns>
|
|
''' <remarks></remarks>
|
|
Public Function GetDatatable(sqlcommand As String, Optional Timeout As Integer = 120) As DataTable
|
|
Try
|
|
If TestCanConnect() = False Then
|
|
Return Nothing
|
|
End If
|
|
|
|
Using oConnection = GetSQLConnection()
|
|
Using oSQLCOmmand = oConnection.CreateCommand()
|
|
oSQLCOmmand.CommandText = sqlcommand
|
|
oSQLCOmmand.CommandTimeout = Timeout
|
|
|
|
Dim dt As DataTable = New DataTable()
|
|
Dim oAdapter As SqlDataAdapter = New SqlDataAdapter(oSQLCOmmand)
|
|
oAdapter.Fill(dt)
|
|
Return dt
|
|
End Using
|
|
End Using
|
|
Catch ex As Exception
|
|
_Logger.Error(ex)
|
|
_Logger.Debug("sqlcommand: " & sqlcommand)
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
''' <summary>
|
|
''' Executes the passed sql-statement
|
|
''' </summary>
|
|
''' <param name="executeStatement">the sql statement</param>
|
|
''' <param name="Timeout">Optional Timeout</param>
|
|
''' <returns>Returns true if properly executed, else false</returns>
|
|
''' <remarks></remarks>
|
|
Public Function NewExecutenonQuery(executeStatement As String, Optional Timeout As Integer = 120) As Boolean
|
|
Try
|
|
If TestCanConnect() = False Then
|
|
Return Nothing
|
|
End If
|
|
|
|
Using oConnection = GetSQLConnection()
|
|
Using oSQLCOmmand = oConnection.CreateCommand()
|
|
oSQLCOmmand.CommandText = executeStatement
|
|
oSQLCOmmand.CommandTimeout = Timeout
|
|
oSQLCOmmand.ExecuteNonQuery()
|
|
Return True
|
|
End Using
|
|
End Using
|
|
Catch ex As Exception
|
|
_Logger.Error(ex)
|
|
_Logger.Debug("executeStatement: " & executeStatement)
|
|
Return False
|
|
End Try
|
|
End Function
|
|
|
|
''' <summary>
|
|
''' Executes the passed sql-statement as Scalar
|
|
''' </summary>
|
|
''' <param name="ScalarSQL">the sql statement</param>
|
|
''' <param name="Timeout">Optional Timeout</param>
|
|
''' <returns>Returns true if properly executed, else false</returns>
|
|
''' <remarks></remarks>
|
|
Public Function NewExecuteScalar(ScalarSQL As String, Optional Timeout As Integer = 120) As Object
|
|
Try
|
|
If TestCanConnect() = False Then
|
|
Return Nothing
|
|
End If
|
|
|
|
Using oConnection As SqlConnection = GetSQLConnection()
|
|
Using oSQLCOmmand = oConnection.CreateCommand()
|
|
oSQLCOmmand.CommandText = ScalarSQL
|
|
oSQLCOmmand.CommandTimeout = Timeout
|
|
Dim oResult As Object = oSQLCOmmand.ExecuteScalar()
|
|
Return oResult
|
|
End Using
|
|
End Using
|
|
Catch ex As Exception
|
|
_Logger.Error(ex)
|
|
_Logger.Debug("executeStatement: " & ScalarSQL)
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
|
|
''' <summary>
|
|
''' Executes the passed sql-statement in asyncmode
|
|
''' </summary>
|
|
''' <param name="executeStatement">the sql statement</param>
|
|
''' <param name="commandtimeout">Optional Timeout</param>
|
|
''' <remarks></remarks>
|
|
Public Sub NewExecuteNonQueryAsync(executeStatement As String, Optional commandtimeout As Integer = 120)
|
|
If TestCanConnect() = False Then
|
|
Exit Sub
|
|
End If
|
|
|
|
Try
|
|
Dim oCallback As New AsyncCallback(AddressOf NewExecuteNonQueryAsync_Callback)
|
|
|
|
Using oConnection As SqlConnection = GetSQLConnection()
|
|
Using oSQLCOmmand = oConnection.CreateCommand()
|
|
oSQLCOmmand.CommandText = executeStatement
|
|
oSQLCOmmand.CommandTimeout = commandtimeout
|
|
oSQLCOmmand.BeginExecuteNonQuery(oCallback, oSQLCOmmand)
|
|
End Using
|
|
End Using
|
|
Catch ex As Exception
|
|
_Logger.Error(ex)
|
|
_Logger.Debug("executeStatement: " & executeStatement)
|
|
End Try
|
|
End Sub
|
|
|
|
Private Sub NewExecuteNonQueryAsync_Callback(ByVal result As IAsyncResult)
|
|
Dim command As SqlCommand = CType(result.AsyncState, SqlCommand)
|
|
Dim res = command.EndExecuteNonQuery(result)
|
|
_Logger.Info("Finished executing Async database operation: {0}", command.CommandText)
|
|
End Sub
|
|
End Class
|