117 lines
4.7 KiB
VB.net
117 lines
4.7 KiB
VB.net
Imports DigitalData.Modules.Database
|
|
Imports DigitalData.Modules.Filesystem
|
|
Imports DigitalData.Modules.Logging
|
|
|
|
Public Class frmConfigDatabase
|
|
Private Const STRING_CONNECTION_SUCCESSFUL = "Die Verbindung wurde erfolgreich aufgebaut!" & vbNewLine & "Möchten Sie diese Verbindung nun in der Anwendung speichern?"
|
|
|
|
Private Logger As Logger = My.LogConfig.GetLogger()
|
|
|
|
Private Sub frmConfigDatabase_Load(sender As Object, e As EventArgs) Handles MyBase.Load
|
|
Dim oConnectionString = My.SystemConfig.ConnectionString
|
|
|
|
If Not oConnectionString = String.Empty Then
|
|
Dim oBuilder = My.SystemConfig.GetConnectionStringBuilder(oConnectionString)
|
|
|
|
If oBuilder Is Nothing Then
|
|
MsgBox("Connection String ist ungültig!", MsgBoxStyle.Critical)
|
|
Exit Sub
|
|
End If
|
|
|
|
If oConnectionString.Contains("Trusted") Then
|
|
chkWinAuth.Checked = True
|
|
txtConnectionString.Text = oConnectionString
|
|
Else
|
|
chkWinAuth.Checked = False
|
|
txtConnectionString.Text = oConnectionString.Replace(oBuilder.Password, "XXXXXX")
|
|
txtUserName.Text = oBuilder.UserID
|
|
End If
|
|
|
|
txtServerName.Text = oBuilder.DataSource
|
|
cmbDatabase.Text = oBuilder.InitialCatalog
|
|
End If
|
|
End Sub
|
|
|
|
Private Sub chkWinAuth_CheckedChanged(sender As Object, e As EventArgs) Handles chkWinAuth.CheckedChanged
|
|
txtPassword.Enabled = Not chkWinAuth.Checked
|
|
txtUserName.Enabled = Not chkWinAuth.Checked
|
|
End Sub
|
|
|
|
Private Sub cmbDatabase_Click(sender As Object, e As EventArgs) Handles cmbDatabase.Click
|
|
Cursor = Cursors.WaitCursor
|
|
|
|
Dim oConnectionString As String = GetConnectionString(False)
|
|
Dim oDatabase As New MSSQLServer(My.LogConfig, oConnectionString)
|
|
|
|
If oDatabase.DBInitialized = False Then
|
|
MsgBox("Verbindung fehlgeschlagen!", MsgBoxStyle.Critical)
|
|
Exit Sub
|
|
End If
|
|
|
|
Try
|
|
Using oConnection = New SqlClient.SqlConnection(oConnectionString)
|
|
oConnection.Open()
|
|
Using cmd As New SqlClient.SqlCommand("sp_databases", oConnection)
|
|
Using dr As SqlClient.SqlDataReader = cmd.ExecuteReader
|
|
If dr.HasRows Then
|
|
cmbDatabase.Properties.Items.Clear()
|
|
|
|
Do While dr.Read
|
|
cmbDatabase.Properties.Items.Add(dr("Database_Name"))
|
|
Loop
|
|
cmbDatabase.ShowPopup()
|
|
Else
|
|
MsgBox("The standard-databases could not be retrieved. The default database will be set!" & vbNewLine & "Check rights in sql-server for user: " & Me.txtUserName.Text, MsgBoxStyle.Exclamation)
|
|
End If
|
|
End Using
|
|
End Using
|
|
End Using
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
End Try
|
|
|
|
Cursor = Cursors.Default
|
|
End Sub
|
|
|
|
Private Function GetConnectionString(WithDatabase As Boolean) As String
|
|
Dim oConnectionString As String
|
|
|
|
If chkWinAuth.Checked Then
|
|
oConnectionString = $"Data Source={txtServerName.Text};Trusted_Connection=True;"
|
|
Else
|
|
oConnectionString = $"Server={txtServerName.Text};User Id={txtUserName.Text};Password={txtPassword.Text};"
|
|
End If
|
|
|
|
If WithDatabase Then
|
|
oConnectionString &= $"Database={cmbDatabase.Text};"
|
|
End If
|
|
|
|
Return oConnectionString
|
|
End Function
|
|
|
|
Private Sub btnTestConnection_Click(sender As Object, e As EventArgs) Handles btnTestConnection.Click
|
|
Try
|
|
Dim oConnectionString = GetConnectionString(True)
|
|
|
|
Using oConnection As New SqlClient.SqlConnection(oConnectionString)
|
|
oConnection.Open()
|
|
oConnection.Close()
|
|
End Using
|
|
|
|
Dim oResult = MessageBox.Show(STRING_CONNECTION_SUCCESSFUL, Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
|
|
|
|
If oResult = DialogResult.Yes Then
|
|
Dim oCrypt As New EncryptionLegacy("!35452didalog=")
|
|
Dim oEncryptedPassword = oCrypt.EncryptData(txtPassword.Text)
|
|
Dim oEncryptedConnectionString = $"Server={txtServerName.Text};Database={cmbDatabase.Text};User Id={txtUserName.Text};Password={oEncryptedPassword};"
|
|
|
|
My.SystemConfig.ConnectionString = oEncryptedConnectionString
|
|
My.SystemConfigManager.Save()
|
|
End If
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
MsgBox("Error while connecting to Database")
|
|
End Try
|
|
End Sub
|
|
End Class |