Imports DigitalData.Modules.Logging
Imports Oracle.ManagedDataAccess.Client
Public Class Oracle
Public DBInitialized As Boolean = False
Public CurrentOracleConnectionString As String = ""
Private _Timeout As Integer
Private _Logger As Logger
Public Sub New(LogConfig As LogConfig, ConnectionString As String, Optional Timeout As Integer = 120)
_Timeout = Timeout
_Logger = LogConfig.GetLogger()
CurrentOracleConnectionString = ConnectionString
DBInitialized = TestCanConnect(CurrentOracleConnectionString)
End Sub
Public Sub New(LogConfig As LogConfig, Server As String, Database As String, UserId As String, Password As String, Optional Timeout As Integer = 120)
_Timeout = Timeout
_Logger = LogConfig.GetLogger()
CurrentOracleConnectionString = GetConnectionString(Server, Database, UserId, Password)
DBInitialized = TestCanConnect(CurrentOracleConnectionString)
End Sub
Private Function TestCanConnect(ConnectionString As String) As Boolean
Try
Dim oSQLconnect As New OracleConnection
oSQLconnect.ConnectionString = ConnectionString
oSQLconnect.Open()
oSQLconnect.Close()
Return True
Catch ex As Exception
_Logger.Error(ex)
Return False
End Try
End Function
Public Shared Function GetConnectionString(Server As String, Database As String, UserId As String, Password As String) As String
Dim oConnectionString = $"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={Server})(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME={Database})));User Id={UserId};Password={Password};"
Return oConnectionString
End Function
'''
''' Returns a datatable for a sql-statement
'''
''' sqlcommand for datatable (select XYZ from TableORView)
''' Returns a datatable
Public Function GetDatatable(sqlcommand As String) As DataTable
Try
Dim oSQLconnect As New OracleConnection
Dim oSQLCOmmand As OracleCommand
oSQLconnect.ConnectionString = CurrentOracleConnectionString
oSQLconnect.Open()
oSQLCOmmand = oSQLconnect.CreateCommand()
oSQLCOmmand.CommandText = sqlcommand
oSQLCOmmand.CommandTimeout = _Timeout
Dim adapter1 As OracleDataAdapter = New OracleDataAdapter(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
''' Returns true if properly executed, else false
Public Function NewExecutenonQuery(executeStatement As String) As Boolean
Try
Dim oSQLconnect As New OracleConnection
Dim oSQLCOmmand As OracleCommand
oSQLconnect.ConnectionString = CurrentOracleConnectionString
oSQLconnect.Open()
oSQLCOmmand = oSQLconnect.CreateCommand()
oSQLCOmmand.CommandText = executeStatement
oSQLCOmmand.CommandTimeout = _Timeout
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 as Scalar
'''
''' the sql statement
''' Returns the scalarvalue
Public Function NewExecuteScalar(executeStatement As String)
Dim result
Try
Dim oSQLconnect As New OracleConnection
Dim oSQLCOmmand As OracleCommand
oSQLconnect.ConnectionString = CurrentOracleConnectionString
oSQLconnect.Open()
oSQLCOmmand = oSQLconnect.CreateCommand()
oSQLCOmmand.CommandText = executeStatement
oSQLCOmmand.CommandTimeout = _Timeout
result = oSQLCOmmand.ExecuteScalar()
oSQLCOmmand.Dispose()
oSQLconnect.Close()
Return result
Catch ex As Exception
_Logger.Error(ex)
_Logger.Debug("executeStatement: " & executeStatement)
Throw ex
End Try
End Function
End Class