Wednesday, May 9, 2012

C# Source for Update app.config

การเขียนฟังก์ชั่นนี้ผมได้สรรหา Code เพราะต้องการ Update app.config file ส่วน Connection String ก็หาอยู่หลายวันเหมือนกันแต่สุดท้ายก็เจอ Source Code ที่ค้นหาอยู่เลยอยากเก็บไว้เป็นตัวอย่างต่อๆไปครับ
ตัวอย่างนี้เป็นการเชื่อมกับฐานข้อมูล MySQL Server นะครับ
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="Easy4Menu.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <connectionStrings>
    <add name="hayeConnectionString" connectionString="server=192.168.1.55;port=3306;user id=root;Password=1234;persist security info=True;database=restaurant_dbo"
  </connectionStrings>
  <appSettings>
    <add key="ServerIP" value="127.0.0.1"/>
  </appSettings>
</configuration>
ตัวอย่างฟอร์ม Update app.config example source code…
App.config Example form
app.config update form
ตัวอย่าง Source Code สำหรับ Update app.config file
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Configuration;
//using System.Configuration.Install;
using System.Xml;
using System.ComponentModel;
using MySql.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Your_name_space
{
    public partial class frmServerConfig : DevExpress.XtraEditors.XtraForm
    {
        private string connectionString;
        string sDataSource;
        string sUid;
        string sPwd;
        public frmServerConfig()
        {
            InitializeComponent();
            string s = ConfigurationManager.ConnectionStrings["hayeConnectionString"].ConnectionString;//Properties.Settings.Default.MyConnection;
            string[] sl = s.Split(';');
            sDataSource = sl[0].Remove(0, 7);
            sUid = sl[2].Remove(0, 8);
            sPwd = sl[3].Remove(0, 9);
            textBox1.Text = sDataSource;
            textBox2.Text = sUid;
            textBox3.Text = sPwd;
            textBox5.Text = sl[1].Remove(0, 5); ;
            textBox4.Text = sl[5].Remove(0, 9); ;
            textBox1.Focus();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            connectionString = "";
            connectionString = "server=" + textBox1.Text + ";port=" + textBox5.Text
                + ";Initial Catalog=" + textBox4.Text  + ";Persist Security Info=True;user id=" + textBox2.Text
                + ";Password=" + textBox3.Text;
            Configuration c = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);
            ConnectionStringsSection section = (ConnectionStringsSection)c.GetSection("connectionStrings");
            section.ConnectionStrings["hayeConnectionString"].ConnectionString = connectionString;
            c.Save();
            Application.Restart();
        }
        private void frmServerConfig_FormClosing(object sender, FormClosingEventArgs e)
        {
            Program.frmserverconfig = null;
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
        }
    }
}
ref : http://www.codemarts.com/2010/06/c-source-for-update-app-config/

No comments:

Post a Comment