FileFlow/Global_Indexer/ClassDatabase.vb
2018-06-29 12:58:47 +02:00

263 lines
11 KiB
VB.net

Imports System.Data.SqlClient
Imports Oracle.ManagedDataAccess.Client
Public Class ClassDatabase
Private Const NETWORK_TIMEOUT As Integer = -1
Private Shared SQLSERVERConnectionString As String
Private Shared OracleConnectionString As String
Public Shared DatabaseConnectionTimeout As Boolean = False
Public Shared Function Init()
Try
SQLSERVERConnectionString = MyConnectionString
Dim SQLconnect As New SqlConnection
SQLconnect.ConnectionString = SQLSERVERConnectionString
SQLconnect.Open()
SQLconnect.Close()
Return True
Catch ex As Exception
ClassLogger.Add("Unexpected error in Database-Init: " & ex.Message, True)
Return False
End Try
End Function
Public Shared Function Return_Datatable(sql_command As String, Optional userInput As Boolean = False)
Try
Dim SQLconnect As New SqlConnection
Dim SQLcommand As SqlCommand
SQLconnect.ConnectionString = SQLSERVERConnectionString
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = sql_command
Dim adapter1 As SqlDataAdapter = New SqlDataAdapter(SQLcommand)
Dim dt As DataTable = New DataTable()
adapter1.Fill(dt)
SQLconnect.Close()
' Reset timeout counter when query was sucessful
DatabaseConnectionTimeout = False
Return dt
Catch ex As SqlException
Dim handled = CatchDatabaseTimeout(ex, sql_command)
If Not handled Then
If userInput = True Then
MsgBox("Error in Return_Datatable - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & sql_command, MsgBoxStyle.Critical)
End If
ClassLogger.Add("Unexpected error in Return_Datatable: " & ex.Message, True)
ClassLogger.Add("#SQL: " & sql_command, False)
End If
Return False
Catch ex As Exception
If userInput = True Then
MsgBox("Error in Return Datatable - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & sql_command, MsgBoxStyle.Critical)
End If
ClassLogger.Add("Unexpected error in Return_Datatable: " & ex.Message, True)
ClassLogger.Add("#SQL: " & sql_command, False)
Return Nothing
End Try
End Function
Public Shared Function Return_Datatable_CS(sql_command As String, ConString As String, Optional userInput As Boolean = False)
Try
Dim SQLconnect As New SqlConnection
Dim SQLcommand As SqlCommand
SQLconnect.ConnectionString = ConString
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = sql_command
Dim adapter1 As SqlDataAdapter = New SqlDataAdapter(SQLcommand)
Dim dt As DataTable = New DataTable()
adapter1.Fill(dt)
SQLconnect.Close()
' Reset timeout counter when query was sucessful
DatabaseConnectionTimeout = False
Return dt
Catch ex As SqlException
Dim handled = CatchDatabaseTimeout(ex, sql_command)
If Not handled Then
If userInput = True Then
MsgBox("Error in Return_Datatable_CS - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & sql_command, MsgBoxStyle.Critical)
End If
ClassLogger.Add("Unexpected error in Return_Datatable_CS: " & ex.Message, True)
ClassLogger.Add("#SQL: " & sql_command, False)
End If
Return False
Catch ex As Exception
If userInput = True Then
MsgBox("Error in Return_Datatable_CS - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & sql_command, MsgBoxStyle.Critical)
End If
ClassLogger.Add("Unexpected error in Return_Datatable_CS: " & ex.Message, True)
ClassLogger.Add("#SQL: " & sql_command, False)
Return Nothing
End Try
End Function
Public Shared Function Execute_non_Query(sql_command As String, Optional userInput As Boolean = False)
Try
Dim SQLconnect As New SqlConnection
Dim SQLcommand As SqlCommand
SQLconnect.ConnectionString = SQLSERVERConnectionString
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
'Update Last Created Record in Foo
SQLcommand.CommandText = sql_command
SQLcommand.ExecuteNonQuery()
SQLcommand.Dispose()
SQLconnect.Close()
' Reset timeout counter when query was sucessful
DatabaseConnectionTimeout = False
Return True
Catch ex As SqlException
Dim handled = CatchDatabaseTimeout(ex, sql_command)
If Not handled Then
If userInput = True Then
MsgBox("Error in Execute non query - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & sql_command, MsgBoxStyle.Critical)
End If
ClassLogger.Add("Unexpected error in Execute_non_Query: " & ex.Message, True)
ClassLogger.Add("#SQL: " & sql_command, False)
End If
Return False
Catch ex As Exception
If userInput = True Then
MsgBox("Error in Execute non query - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & sql_command, MsgBoxStyle.Critical)
End If
ClassLogger.Add("Unexpected error in Execute_non_Query: " & ex.Message, True)
ClassLogger.Add("#SQL: " & sql_command, False)
Return False
End Try
End Function
Public Shared Function Execute_Scalar(sql_command As String, ConString As String, Optional userInput As Boolean = False)
Dim result
Try
Dim SQLconnect As New SqlConnection
Dim SQLcommand As SqlCommand
SQLconnect.ConnectionString = ConString
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
'Update Last Created Record in Foo
SQLcommand.CommandText = sql_command
result = SQLcommand.ExecuteScalar()
SQLcommand.Dispose()
SQLconnect.Close()
' Reset timeout counter when query was sucessful
DatabaseConnectionTimeout = False
Return result
Catch ex As SqlException
Dim handled = CatchDatabaseTimeout(ex, sql_command)
If Not handled Then
If userInput = True Then
MsgBox("Error in Execute non query - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & sql_command, MsgBoxStyle.Critical)
End If
ClassLogger.Add("Unexpected error in Execute_non_Query: " & ex.Message, True)
ClassLogger.Add("#SQL: " & sql_command, False)
End If
Return False
Catch ex As Exception
If userInput = True Then
MsgBox("Error in Execute Scalar - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & sql_command, MsgBoxStyle.Critical)
End If
ClassLogger.Add("Unexpected error in Execute_Scalar: " & ex.Message, True)
ClassLogger.Add("#SQL: " & sql_command, False)
Return Nothing
End Try
End Function
Public Shared Function OracleExecute_Scalar(cmdscalar As String, OracleConnection As String)
Dim result
Try
Dim SQLconnect As New OracleConnection
Dim SQLcommand As New OracleCommand
SQLconnect.ConnectionString = OracleConnection
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
'Update Last Created Record in Foo
SQLcommand.CommandText = cmdscalar
result = SQLcommand.ExecuteScalar()
SQLcommand.Dispose()
SQLconnect.Close()
Return result
Catch ex As Exception
ClassLogger.Add("Unexpected error in OracleExecute_Scalar: " & ex.Message, True)
ClassLogger.Add("#SQL: " & cmdscalar, False)
Return Nothing
End Try
End Function
Public Shared Function OracleExecute_non_Query(ExecuteCMD As String, OracleConnection As String, Optional userInput As Boolean = False)
Try
Dim SQLconnect As New OracleConnection
Dim SQLcommand As OracleCommand
SQLconnect.ConnectionString = OracleConnection
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
'Update Last Created Record in Foo
SQLcommand.CommandText = ExecuteCMD
SQLcommand.ExecuteNonQuery()
SQLcommand.Dispose()
SQLconnect.Close()
Return True
Catch ex As Exception
If userInput = True Then
MsgBox("Error in OracleExecute_non_Query - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & ExecuteCMD, MsgBoxStyle.Critical)
End If
ClassLogger.Add("Unexpected error in OracleExecute_non_Query: " & ex.Message, True)
ClassLogger.Add("#SQL: " & ExecuteCMD, False)
Return False
End Try
End Function
Public Shared Function Oracle_Return_Datatable(Select_anweisung As String, OracleConnection As String, Optional userInput As Boolean = False)
Try
Dim SQLconnect As New OracleConnection
Dim SQLcommand As OracleCommand
SQLconnect.ConnectionString = OracleConnection
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = Select_anweisung
Dim adapter1 As OracleDataAdapter = New OracleDataAdapter(SQLcommand)
Dim dt As DataTable = New DataTable()
adapter1.Fill(dt)
SQLconnect.Close()
Return dt
Catch ex As Exception
If userInput = True Then
MsgBox("Error in Oracle Return Datatable - Error-Message:" & vbNewLine & ex.Message & vbNewLine & "SQL-Command:" & vbNewLine & Select_anweisung, MsgBoxStyle.Critical)
End If
ClassLogger.Add("Unexpected error in Oracle_Return_Datatable: " & ex.Message, True)
ClassLogger.Add("#SQL: " & Select_anweisung, False)
Return Nothing
End Try
End Function
Public Shared Function CatchDatabaseTimeout(ex As SqlException, sql_command As String)
Dim FatalErrors As New List(Of Integer) From {-1, -2, 121}
If FatalErrors.Contains(ex.Number) Then
DatabaseConnectionTimeout = True
ClassLogger.Add("Network timeout error in Return_Datatable: " & ex.Message, True)
ClassLogger.Add("#SQL: " & sql_command, False)
Return True
Else
Return False
End If
End Function
End Class