| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 | using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.Threading;using System.Windows.Forms;using DevExpress.XtraEditors;using GCAS.Code;using GCAS.Dto;using GCAS.Localization;using GCAS.Model;using MySql.Data.MySqlClient;namespace GCAS{    public partial class ServerSet : XtraForm    {        private List<NameValue> L;        private ActionsRepository actionsRepository;        public OperatorModel currentUser { get; set; }        public ServerSet()        {            InitializeComponent();            L = LocalizationHelper.GetSource(Thread.CurrentThread.CurrentUICulture);        }        private void ServerSet_Load(object sender, EventArgs e)        {            actionsRepository = new ActionsRepository();            string connString = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)?.ConnectionStrings?.ConnectionStrings["myconn"]?.ToString();            if (!string.IsNullOrEmpty(connString))            {                try                {                    var spitArr = connString.Split(';');                    text_ip.Text = spitArr[0].Split('=')[1];                    text_port.Text = spitArr[1].Split('=')[1];                    text_db.Text = spitArr[2].Split('=')[1];                    text_name.Text = spitArr[3].Split('=')[1];                    text_pwd.Text = spitArr[4].Split('=')[1];                }                catch { }            }        }        private string Valid()        {            if (text_ip.Text.Trim() == string.Empty)            {                XtraMessageBox.Show(L.GetString("hostIsNotEmpty"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return null;            }            if (text_port.Text.Trim() == string.Empty)            {                XtraMessageBox.Show(L.GetString("portIsNotEmpty"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return null;            }            if (text_db.Text.Trim() == string.Empty)            {                XtraMessageBox.Show(L.GetString("databaseIsNotEmpty"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return null;            }            if (text_name.Text.Trim() == string.Empty)            {                XtraMessageBox.Show(L.GetString("usernameIsNotEmpty"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return null;            }            if (text_name.Text.Trim() == string.Empty)            {                XtraMessageBox.Show(L.GetString("pwdIsNotEmpty"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return null;            }            return $"server={text_ip.Text.Trim()};port={text_port.Text.Trim()};database={text_db.Text.Trim()};user={text_name.Text.Trim()};password={text_pwd.Text.Trim()};charset=utf8";        }        private void btn_ok_Click(object sender, EventArgs e)        {            var connString = Valid();            IDbConnection conn = new MySqlConnection(connString);            try            {                conn.Open();                if (conn.State == ConnectionState.Open)                {                    XtraMessageBox.Show(L.GetString("connectionSucceeded"), L.GetString("notification"), MessageBoxButtons.OK, MessageBoxIcon.Information);                    DialogResult = DialogResult.OK;                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);                    if (string.IsNullOrEmpty(ConfigurationManager.ConnectionStrings["myconn"]?.ConnectionString))                    {                        config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings("myconn", connString));//增加                    }                    else                    {                        config.ConnectionStrings.ConnectionStrings["myconn"].ConnectionString = connString;//修改                     }                    config.Save(ConfigurationSaveMode.Modified);                    ConfigurationManager.RefreshSection("ConnectionStrings");                    Close();                }                else                {                    XtraMessageBox.Show(L.GetString("failedConnection"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);                    return;                }            }            catch            {                XtraMessageBox.Show(L.GetString("failedConnection"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);                return;            }            finally            {                conn?.Close();                conn.Dispose();            }        }        private void text_ip_Click(object sender, EventArgs e)        {            SoftKeyboard.ActiveSoftKeyboard(sender);        }        private void text_port_Click(object sender, EventArgs e)        {            SoftKeyboard.ActiveSoftKeyboard(sender);        }        private void text_db_Click(object sender, EventArgs e)        {            SoftKeyboard.ActiveSoftKeyboard(sender);        }        private void text_name_Click(object sender, EventArgs e)        {            SoftKeyboard.ActiveSoftKeyboard(sender);        }        private void text_pwd_Click(object sender, EventArgs e)        {            SoftKeyboard.ActiveSoftKeyboard(sender);        }    }}
 |