Custom Search
Your Ad Here
Latest News

Mysql class Database Settings Connection

11/20/08 , Posted by Gregfox at 8:08 PM

Imports System.Configuration
Public Class DS_frmDatabaseSettings
Private Sub btnTestConnection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestConnection.Click
Dim strErrMsg As String = String.Empty
Dim RptString As String = String.Empty

Try
MySQLDBAccess.connectionString = "server = " & txtServer.Text & ";" _
& "user id = " & txtUserID.Text & ";" _
& "password = " & txtPassword.Text & ";" _
& "database = " & txtDatabase.Text

MySQLDBAccess.TestMySQLDBConnection(strErrMsg)


If strErrMsg = String.Empty Then
'Write DB Connection Setting to app.config
WriteDBConfig(MySQLDBAccess.connectionString)
MessageBox.Show("Test connection successful.", "DiabNet DB Connection", MessageBoxButtons.OK, MessageBoxIcon.Information)
btnOK.Enabled = True
Else
MessageBox.Show(strErrMsg, "TEST DB Connection", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
DBL_PRINTLOG("DS_frmDatabaseSettings::btnTestConnection_Click() Exception >> " & ex.Message)
End Try
End Sub

REM Write DB Connection Setting to app.config
Private Sub WriteDBConfig(ByVal strConnString As String)

' Open app.config of executable
Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

If ConfigurationManager.AppSettings.HasKeys() Then
' Clear key list
config.AppSettings.Settings.Clear()
End If

' Add application setting
config.AppSettings.Settings.Add("MySQLConnString", strConnString)
config.AppSettings.Settings.Add("Server", txtServer.Text)
config.AppSettings.Settings.Add("UserID", txtUserID.Text)
config.AppSettings.Settings.Add("Password", txtPassword.Text)
config.AppSettings.Settings.Add("Database", txtDatabase.Text)

' Save the configuration file
config.Save(ConfigurationSaveMode.Modified)

' Force reload of changed section
ConfigurationManager.RefreshSection("appSettings")
End Sub

REM Read DB Connection Settings from app.config
Private Function ReadDBConfig(ByVal strKey As String) As String
Dim strKeyValue As String = String.Empty
Dim bKeyFound As Boolean = False

If ConfigurationManager.AppSettings.HasKeys() Then
For nKeyIndex As Integer = 0 To (ConfigurationManager.AppSettings.Keys.Count - 1)
If ConfigurationManager.AppSettings.GetKey(nKeyIndex) = strKey Then
strKeyValue = ConfigurationManager.AppSettings(strKey)
bKeyFound = True
Exit For
End If
Next
End If

Return strKeyValue
End Function

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
MySQLDBAccess.connectionString = "server = " & txtServer.Text & ";" _
& "user id = " & txtUserID.Text & ";" _
& "password = " & txtPassword.Text & ";" _
& "database = " & txtDatabase.Text

WriteDBConfig(MySQLDBAccess.connectionString)

Me.Hide()
End Sub

Private Sub DS_frmDatabaseSettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Retrieve DB Connection Settings from app.config
txtServer.Text = ReadDBConfig("Server")
txtUserID.Text = ReadDBConfig("UserID")
txtPassword.Text = ReadDBConfig("Password")
txtDatabase.Text = ReadDBConfig("Database")
btnOK.Enabled = False
End Sub
End Class

Currently have 0 comments:

Leave a Reply

Post a Comment