รายละเอียดของการตอบ ::
ตอนนี้สามารถรันไฟล์ได้แล้วค่ะ แต่ไม่รู้จะเปิดconnection ยังไงรบกวนด้วยน่ะค่ะ
code>>>frmChaneSettings
Public Class frmChaneSettings
Private Sub frmChaneSettings_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
LoadConfigValueToControls()
End Sub
''' <summary>
''' This Will set the appConfigs Paramters values to Text box Controls
''' </summary>
''' <remarks></remarks>
Private Sub LoadConfigValueToControls()
txtServerName.Text = System.Configuration.ConfigurationSettings.AppSettings.Get("DBServerName")
txtDBName.Text = System.Configuration.ConfigurationSettings.AppSettings.Get("DatabaseName")
txtDBUserID.Text = System.Configuration.ConfigurationSettings.AppSettings.Get("DatabaseUserID")
txtDBPwd.Text = System.Configuration.ConfigurationSettings.AppSettings.Get("DatabasePwd")
End Sub
Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click
AppConfigFileSettings.UpdateAppSettings("DBServerName", txtServerName.Text)
AppConfigFileSettings.UpdateAppSettings("DatabaseName", txtDBName.Text)
AppConfigFileSettings.UpdateAppSettings("DatabaseUserID", txtDBUserID.Text)
AppConfigFileSettings.UpdateAppSettings("DatabasePwd", txtDBPwd.Text)
MsgBox("Application Settings has been Changed successfully.")
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Application.Exit()
End Sub
code >> AppConfigFileSettings.vb
Imports System.Configuration
Imports System.Xml
''' <summary>
''' AppConfigFileSettings: This class is used to Change the AppConfigs Paramters at rumtime through User Interface
''' </summary>
''' <remarks></remarks>
Public Class AppConfigFileSettings
''' <summary>
''' UpdateAppSettings: It will update the app.Config file AppConfig key values
''' </summary>
''' <param name="KeyName">AppConfigs KeyName</param>
''' <param name="KeyValue">AppConfigs KeyValue</param>
''' <remarks></remarks>
Public Shared Sub UpdateAppSettings(ByVal KeyName As String, ByVal KeyValue As String)
Dim XmlDoc As New XmlDocument()
XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
For Each xElement As XmlElement In XmlDoc.DocumentElement
If xElement.Name = "appSettings" Then
For Each xNode As XmlNode In xElement.ChildNodes
If xNode.Attributes(0).Value = KeyName Then
xNode.Attributes(1).Value = KeyValue
End If
Next
End If
Next
XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
End Sub
End Class
code >> app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DBServerName" value="Localhost"></add>
<add key="DatabaseName" value="TestDB"></add>
<add key="DatabaseUserID" value="sa"></add>
<add key="DatabasePwd" value="sa"></add>
</appSettings>
<configSections>
</configSections>
<system.diagnostics>
<sources>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLog"/>
<!-- Uncomment the below section to write to the Application Event Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information" />
</switches>
<sharedListeners>
<add name="FileLog"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"/>
<!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
</sharedListeners>
</system.diagnostics>
</configuration>