Imports System.Data.SqlClient Public Class SQLServer Private Shared Logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger Public DBInitialized As Boolean = False Public CurrentSQLConnectionString As String = "" Public Sub New(CONSTRING As String) Init(CONSTRING) End Sub Public Function Init(CONSTRING As String) Try Dim oSQLconnect As New SqlClient.SqlConnection oSQLconnect.ConnectionString = CONSTRING oSQLconnect.Open() oSQLconnect.Close() CurrentSQLConnectionString = CONSTRING DBInitialized = True Return True Catch ex As Exception Logger.Error(ex) 'clsLogger.Add("Error in DatabaseInit: " & ex.Message, True) Return False End Try End Function ''' ''' Returns a datatable for a sql-statement ''' ''' sqlcommand for datatable (select XYZ from TableORView) ''' Optional Timeout ''' Returns a datatable ''' Public Function GetDatatable(sqlcommand As String, Optional commandtimeout As Integer = 120) As DataTable Try Dim oSQLconnect As New SqlClient.SqlConnection Dim oSQLCOmmand As SqlClient.SqlCommand oSQLconnect.ConnectionString = CurrentSQLConnectionString oSQLconnect.Open() oSQLCOmmand = oSQLconnect.CreateCommand() oSQLCOmmand.CommandText = sqlcommand oSQLCOmmand.CommandTimeout = commandtimeout Dim adapter1 As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(oSQLCOmmand) Dim dt As DataTable = New DataTable() adapter1.Fill(dt) oSQLconnect.Close() Return dt Catch ex As Exception Logger.Error(ex) Logger.Debug("sqlcommand: " & sqlcommand) Return Nothing End Try End Function ''' ''' Executes the passed sql-statement ''' ''' the sql statement ''' Optional Timeout ''' Returns true if properly executed, else false ''' Public Function NewExecutenonQuery(executeStatement As String, Optional commandtimeout As Integer = 120) As Boolean Try Dim oSQLconnect As New SqlClient.SqlConnection Dim oSQLCOmmand As SqlClient.SqlCommand oSQLconnect.ConnectionString = CurrentSQLConnectionString oSQLconnect.Open() oSQLCOmmand = oSQLconnect.CreateCommand() oSQLCOmmand.CommandText = executeStatement oSQLCOmmand.CommandTimeout = commandtimeout oSQLCOmmand.ExecuteNonQuery() oSQLCOmmand.Dispose() oSQLconnect.Close() Return True Catch ex As Exception Logger.Error(ex) Logger.Debug("executeStatement: " & executeStatement) Return False End Try End Function ''' ''' Executes the passed sql-statement in asyncmode ''' ''' the sql statement ''' Optional Timeout ''' Public Sub NewExecuteNonQueryAsync(executeStatement As String, Optional commandtimeout As Integer = 120) Dim oSQLconnect As New SqlClient.SqlConnection Dim oSQLCOmmand As SqlClient.SqlCommand Dim callback As New AsyncCallback(AddressOf Execute_non_Query_Async_Callback) Try oSQLconnect.ConnectionString = CurrentSQLConnectionString oSQLconnect.Open() oSQLCOmmand = oSQLconnect.CreateCommand() oSQLCOmmand.CommandText = executeStatement oSQLCOmmand.CommandTimeout = commandtimeout oSQLCOmmand.BeginExecuteNonQuery(callback, oSQLCOmmand) Catch ex As Exception Logger.Error(ex) Logger.Debug("executeStatement: " & executeStatement) Finally oSQLCOmmand.Dispose() oSQLconnect.Close() End Try End Sub Private Sub Execute_non_Query_Async_Callback(ByVal result As IAsyncResult) Dim command As SqlClient.SqlCommand = CType(result.AsyncState, SqlClient.SqlCommand) Dim res = command.EndExecuteNonQuery(result) Logger.Info(String.Format("Finished executing Async database operation: {0}", command.CommandText)) End Sub ''' ''' Executes the passed sql-statement as Scalar ''' ''' the sql statement ''' Optional Timeout ''' Returns true if properly executed, else false ''' Public Function NewExecuteScalar(executeStatement As String, Optional commandtimeout As Integer = 120) Dim result Try Dim oSQLconnect As New SqlClient.SqlConnection Dim oSQLCOmmand As SqlClient.SqlCommand oSQLconnect.ConnectionString = CurrentSQLConnectionString oSQLconnect.Open() oSQLCOmmand = oSQLconnect.CreateCommand() oSQLCOmmand.CommandText = executeStatement oSQLCOmmand.CommandTimeout = commandtimeout result = oSQLCOmmand.ExecuteScalar() oSQLCOmmand.Dispose() oSQLconnect.Close() Return result Catch ex As Exception Logger.Error(ex) Logger.Debug("executeStatement: " & executeStatement) Return Nothing End Try End Function End Class