131 lines
5.9 KiB
VB.net
131 lines
5.9 KiB
VB.net
Imports System.IO
|
|
Imports DD_LIB_Standards
|
|
|
|
Public Class frmConfigDatabase
|
|
|
|
Private ConnectionChanged As Boolean = False
|
|
|
|
Private Sub frmConfigDatabase_Load(sender As Object, e As EventArgs) Handles MyBase.Load
|
|
If Not MyConnectionString = String.Empty Then
|
|
ConnectionChanged = False
|
|
|
|
Dim csb As New SqlClient.SqlConnectionStringBuilder
|
|
csb.ConnectionString = MyConnectionString
|
|
|
|
Dim constr = MyConnectionString
|
|
If Not MyConnectionString.Contains("Trusted") Then
|
|
constr = constr.Replace(csb.Password, "XXXXX")
|
|
txtUser.Text = csb.UserID
|
|
chkbxUserAut.Checked = False
|
|
Else
|
|
chkbxUserAut.Checked = True
|
|
End If
|
|
|
|
Try
|
|
txtServer.Text = csb.DataSource
|
|
cmbDatenbank.Text = csb.InitialCatalog
|
|
Catch ex As Exception
|
|
|
|
End Try
|
|
|
|
txtActualConnection.Text = constr
|
|
End If
|
|
End Sub
|
|
|
|
Sub Load_Databases()
|
|
Try
|
|
Me.Cursor = Cursors.WaitCursor
|
|
Dim csb As New SqlClient.SqlConnectionStringBuilder
|
|
csb.DataSource = Me.txtServer.Text
|
|
csb.IntegratedSecurity = False
|
|
csb.UserID = Me.txtUser.Text
|
|
csb.Password = Me.txtPasswort.Text
|
|
|
|
Dim con As String
|
|
If chkbxUserAut.Checked Then
|
|
con = "Data Source=" & Me.txtServer.Text & ";Trusted_Connection=True;"
|
|
Else
|
|
'con = "Server=" & Me.txtServer.Text & ";Database=" & Me.cmbDatenbank.Text & ";User Id=" & Me.txtUser.Text & ";Password=" & Me.txtPasswort.Text & ";"
|
|
con = "Server=" & Me.txtServer.Text & ";User Id=" & Me.txtUser.Text & ";Password=" & Me.txtPasswort.Text & ";"
|
|
End If
|
|
Dim connection As New SqlClient.SqlConnection(con) 'csb.ConnectionString)
|
|
connection.Open()
|
|
Dim cmd As New SqlClient.SqlCommand("sp_databases", connection)
|
|
cmd.CommandType = CommandType.StoredProcedure
|
|
' Ausführen und Ergebnis in einer ListBox anzeigen
|
|
Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader
|
|
If dr.HasRows Then
|
|
cmbDatenbank.Items.Clear()
|
|
Do While dr.Read
|
|
cmbDatenbank.Items.Add(dr("Database_Name"))
|
|
Loop
|
|
cmbDatenbank.DroppedDown = True
|
|
Else
|
|
MsgBox("The standard-databases could not be retrieved. The default database will be set!" & vbNewLine & "Check rights in sql-server for user: " & Me.txtUser.Text, MsgBoxStyle.Exclamation)
|
|
End If
|
|
connection.Close()
|
|
Catch ex As Exception
|
|
If ex.Message.ToLower.Contains("he standard-databases") Or ex.Message.ToLower.Contains("ie standard-datenbanken") Then
|
|
cmbDatenbank.Text = "DD_ECM"
|
|
End If
|
|
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error in Load Databases:")
|
|
End Try
|
|
Cursor = Cursors.Default
|
|
End Sub
|
|
|
|
Private Sub BtnConnect_Click(sender As Object, e As EventArgs) Handles BtnConnect.Click
|
|
Try
|
|
Dim con As String
|
|
If chkbxUserAut.Checked Then
|
|
con = "Server=" & Me.txtServer.Text & ";Database=" & Me.cmbDatenbank.Text & ";Trusted_Connection=True;Application Name=DD UserManager"
|
|
Else
|
|
con = "Server=" & Me.txtServer.Text & ";Database=" & Me.cmbDatenbank.Text & ";User Id=" & Me.txtUser.Text & ";Password=" & Me.txtPasswort.Text & ";Application Name=DD ADDI-Client"
|
|
End If
|
|
|
|
Dim connection As New SqlClient.SqlConnection(con) 'csb.ConnectionString)
|
|
'während Verbindungsaufbau Sanduhr-Mauszeiger
|
|
Cursor = Cursors.WaitCursor
|
|
connection.Open()
|
|
Cursor = Cursors.Default
|
|
'DialogResult = Windows.Forms.DialogResult.OK
|
|
Dim result As MsgBoxResult
|
|
Dim msg = "Die Verbindung wurde erfolgreich aufgebaut!" & vbNewLine & "Möchten Sie diese Verbindung nun in der Anwendung speichern?"
|
|
result = MessageBox.Show(msg, "Datenbank-Verbindung", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
|
|
If result = MsgBoxResult.Yes Then
|
|
ConnectionChanged = True
|
|
'Set the construction string
|
|
MyConnectionString = con
|
|
clsDatabase.Init(MyConnectionString)
|
|
My.Settings.Save()
|
|
|
|
If chkbxUserAut.Checked = False Then
|
|
Dim wrapper As New clsEncryption("!35452didalog=")
|
|
Dim cipherText As String = wrapper.EncryptData(Me.txtPasswort.Text)
|
|
Dim pw As String = cipherText
|
|
con = "Server=" & Me.txtServer.Text & ";Database=" & Me.cmbDatenbank.Text & ";User Id=" & Me.txtUser.Text & ";Password=" & pw & ";"
|
|
End If
|
|
|
|
SaveMySettingsValue("MyConnectionString", con, "ConfigMain")
|
|
|
|
Dim csb As New SqlClient.SqlConnectionStringBuilder
|
|
csb.ConnectionString = MyConnectionString
|
|
Dim constr = connection.ConnectionString
|
|
If chkbxUserAut.Checked = False Then
|
|
constr = constr.Replace(csb.Password, "XXXXX")
|
|
End If
|
|
Me.txtActualConnection.Text = constr
|
|
End If
|
|
Catch ex As Exception
|
|
Cursor = Cursors.Default
|
|
MsgBox("Error in Connectionbuild: " & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
|
|
End Try
|
|
End Sub
|
|
|
|
Private Sub cmbDatenbank_MouseClick(sender As Object, e As MouseEventArgs) Handles cmbDatenbank.MouseClick
|
|
If txtServer.Text = String.Empty Or txtUser.Text = String.Empty Or txtPasswort.Text = String.Empty Then
|
|
MsgBox("Bitte füllen Sie die Felder 'Server-Name', 'Benutzername' und 'Passwort' zuerst aus!", MsgBoxStyle.Exclamation, "Datenbank-Verbindung")
|
|
Else
|
|
Load_Databases()
|
|
End If
|
|
End Sub
|
|
End Class |