Use Database from Monorepo, Add initial ODBC Support

This commit is contained in:
Jonathan Jenne
2019-08-06 15:41:02 +02:00
parent 6e6a483602
commit 43d3ba5ef2
15 changed files with 237 additions and 364 deletions

View File

@@ -1,9 +1,12 @@
Imports System.Data.SqlClient
Imports System.Data.Odbc
Imports Oracle.ManagedDataAccess.Client
Imports Microsoft.Win32
Public Class frmConnection
Private Const PROVIDER_ORACLE = "ORACLE"
Private Const PROVIDER_MSSQL = "MS-SQL"
Private Const PROVIDER_ODBC = "ODBC"
Private Sub TBDD_CONNECTIONBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Validate()
@@ -13,6 +16,17 @@ Public Class frmConnection
Private Sub frmConnection_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Load_Connections()
Dim oSystemConnections = LoadOdbcConnections(Registry.LocalMachine)
Dim oUserConnections = LoadOdbcConnections(Registry.CurrentUser)
For Each oDSN In oSystemConnections
ODBCServerTextBox.Items.Add(oDSN)
Next
For Each oDSN In oUserConnections
ODBCServerTextBox.Items.Add(oDSN)
Next
End Sub
Private Sub Load_Connections()
@@ -25,6 +39,30 @@ Public Class frmConnection
End Try
End Sub
Private Function LoadOdbcConnections(Root As RegistryKey) As List(Of String)
Dim oConnections As New List(Of String)
Try
Dim oPath As String = "SOFTWARE\\ODBC\\ODBC.INI\\ODBC Data Sources"
Dim oKey As RegistryKey = Root.OpenSubKey(oPath, False)
If oKey Is Nothing Then
Return oConnections
End If
For Each oName In oKey.GetValueNames
oConnections.Add(oName)
Next
Return oConnections
Catch ex As Exception
Logger.Error(ex)
Return oConnections
End Try
End Function
Private Sub TBDD_CONNECTIONBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBDD_CONNECTIONBindingSource.AddingNew
MyDataset.TBDD_CONNECTION.AKTIVColumn.DefaultValue = True
MyDataset.TBDD_CONNECTION.ERSTELLTWERColumn.DefaultValue = Environment.UserName
@@ -45,42 +83,43 @@ Public Class frmConnection
oConnectionString = $"Server={Server};Database={Database};User Id={UserId};Password={Password};"
End If
Cursor = Cursors.WaitCursor
Using oConnection As New SqlConnection(oConnectionString)
Try
oConnection.Open()
MsgBox("Die Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, Text)
Catch ex As Exception
Logger.Error(ex)
MsgBox("Fehler beim Verbindungsaufbau: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation, Text)
Finally
Cursor = Cursors.Default
MsgBox("Fehler beim Verbindungsaufbau (MSSQL): " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation, Text)
End Try
End Using
Case PROVIDER_ORACLE
Try
Dim conn As New OracleConnectionStringBuilder
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};"
'conn.DataSource = DATENBANKComboBox.Text
'conn.UserID = txtUserId.Text
'conn.Password = txtPassword.Text
'conn.PersistSecurityInfo = True
'conn.ConnectionTimeout = 120
'oConnectionString = conn.ConnectionString
Using connection As New OracleConnection(oConnectionString)
connection.Open()
MsgBox("Die Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, Text)
End Using
MsgBox("Die Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, Text)
Catch ex As Exception
Logger.Error(ex)
MsgBox(ex.Message & vbNewLine & vbNewLine & oConnectionString, MsgBoxStyle.Critical, "Fehler bei Verbindungsaufbau Oracle:")
MsgBox("Fehler beim Verbindungsaufbau (ORACLE): " & vbNewLine & ex.Message, MsgBoxStyle.Critical, Text)
End Try
Case PROVIDER_ODBC
Try
oConnectionString = $"DSN={Server};UID={UserId};PWD={Password}"
Using oConnection As New OdbcConnection(oConnectionString)
oConnection.Open()
MsgBox("Die Verbindung wurde erfolgreich aufgebaut!", MsgBoxStyle.Information, Text)
End Using
Catch ex As Exception
Logger.Error(ex)
MsgBox("Fehler beim Verbindungsaufbau (ODBC): " & vbNewLine & ex.Message, MsgBoxStyle.Critical, Text)
End Try
End Select
End Sub
Private Sub DATENBANKComboBox_Click(sender As Object, e As EventArgs) Handles DATENBANKComboBox.Click
@@ -97,44 +136,44 @@ Public Class frmConnection
End If
If chkWinAuth.Checked Then
oConnectionBuilder.DataSource = SERVERTextBox.Text
oConnectionBuilder.IntegratedSecurity = True
Else
If USERNAMETextBox.Text = String.Empty Or PASSWORDTextBox.Text = String.Empty Then
MsgBox("Bitte geben sie Benutzer und Passwort an!", MsgBoxStyle.Exclamation, Text)
Exit Sub
End If
oConnectionBuilder.DataSource = SERVERTextBox.Text
oConnectionBuilder.IntegratedSecurity = False
oConnectionBuilder.UserID = USERNAMETextBox.Text
oConnectionBuilder.Password = PASSWORDTextBox.Text
oConnectionBuilder.DataSource = SERVERTextBox.Text
oConnectionBuilder.IntegratedSecurity = True
Else
If USERNAMETextBox.Text = String.Empty Or PASSWORDTextBox.Text = String.Empty Then
MsgBox("Bitte geben sie Benutzer und Passwort an!", MsgBoxStyle.Exclamation, Text)
Exit Sub
End If
Try
Using oConnection As New SqlConnection(oConnectionBuilder.ToString)
oConnection.Open()
oConnectionBuilder.DataSource = SERVERTextBox.Text
oConnectionBuilder.IntegratedSecurity = False
oConnectionBuilder.UserID = USERNAMETextBox.Text
oConnectionBuilder.Password = PASSWORDTextBox.Text
End If
Using oCmd As New SqlCommand("sp_databases", oConnection) With {.CommandType = CommandType.StoredProcedure}
Using oReader As SqlDataReader = oCmd.ExecuteReader
If oReader.HasRows Then
DATENBANKComboBox.Items.Clear()
Do While oReader.Read()
DATENBANKComboBox.Items.Add(oReader.Item("Database_Name"))
Loop
DATENBANKComboBox.DroppedDown = True
Else
MsgBox("The standard-databases could not be retrieved." & vbNewLine & "Check rights in sql-server for user", MsgBoxStyle.Exclamation)
End If
End Using
Try
Using oConnection As New SqlConnection(oConnectionBuilder.ToString)
oConnection.Open()
Using oCmd As New SqlCommand("sp_databases", oConnection) With {.CommandType = CommandType.StoredProcedure}
Using oReader As SqlDataReader = oCmd.ExecuteReader
If oReader.HasRows Then
DATENBANKComboBox.Items.Clear()
Do While oReader.Read()
DATENBANKComboBox.Items.Add(oReader.Item("Database_Name"))
Loop
DATENBANKComboBox.DroppedDown = True
Else
MsgBox("The standard-databases could not be retrieved." & vbNewLine & "Check rights in sql-server for user", MsgBoxStyle.Exclamation)
End If
End Using
End Using
Catch ex As Exception
Logger.Error(ex)
MsgBox("Error while loading Databases:" & vbCrLf & ex.Message, MsgBoxStyle.Critical, Text)
Finally
Cursor = Cursors.Default
End Try
End Using
Catch ex As Exception
Logger.Error(ex)
MsgBox("Error while loading Databases:" & vbCrLf & ex.Message, MsgBoxStyle.Critical, Text)
Finally
Cursor = Cursors.Default
End Try
End Sub
Private Sub chkWinAuth_CheckedChanged(sender As Object, e As EventArgs) Handles chkWinAuth.CheckedChanged
@@ -178,4 +217,23 @@ Public Class frmConnection
Private Sub BarButtonItem1_ItemClick_1(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
ConnectionTest(SQL_PROVIDERComboBox.Text, chkWinAuth.Checked, SERVERTextBox.Text, USERNAMETextBox.Text, PASSWORDTextBox.Text, DATENBANKComboBox.Text)
End Sub
Private Sub SQL_PROVIDERComboBox_SelectedValueChanged(sender As Object, e As EventArgs) Handles SQL_PROVIDERComboBox.SelectedValueChanged
If SQL_PROVIDERComboBox.Text = PROVIDER_ODBC Then
DATENBANKComboBox.Enabled = False
ODBCServerTextBox.Enabled = True
chkWinAuth.Enabled = False
Else
DATENBANKComboBox.Enabled = True
ODBCServerTextBox.Enabled = False
chkWinAuth.Enabled = True
End If
End Sub
Private Sub ODBCServerTextBox_SelectedValueChanged(sender As Object, e As EventArgs) Handles ODBCServerTextBox.SelectedValueChanged
If ODBCServerTextBox.Text <> String.Empty Then
SERVERTextBox.Text = ODBCServerTextBox.Text
BEZEICHNUNGTextBox.Text = ODBCServerTextBox.Text
End If
End Sub
End Class