ServerSet.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7. using DevExpress.XtraEditors;
  8. using GCAS.Code;
  9. using GCAS.Dto;
  10. using GCAS.Localization;
  11. using GCAS.Model;
  12. using MySql.Data.MySqlClient;
  13. namespace GCAS
  14. {
  15. public partial class ServerSet : XtraForm
  16. {
  17. private List<NameValue> L;
  18. private ActionsRepository actionsRepository;
  19. public OperatorModel currentUser { get; set; }
  20. public ServerSet()
  21. {
  22. InitializeComponent();
  23. L = LocalizationHelper.GetSource(Thread.CurrentThread.CurrentUICulture);
  24. }
  25. private void ServerSet_Load(object sender, EventArgs e)
  26. {
  27. actionsRepository = new ActionsRepository();
  28. string connString = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)?.ConnectionStrings?.ConnectionStrings["myconn"]?.ToString();
  29. if (!string.IsNullOrEmpty(connString))
  30. {
  31. try
  32. {
  33. var spitArr = connString.Split(';');
  34. text_ip.Text = spitArr[0].Split('=')[1];
  35. text_port.Text = spitArr[1].Split('=')[1];
  36. text_db.Text = spitArr[2].Split('=')[1];
  37. text_name.Text = spitArr[3].Split('=')[1];
  38. text_pwd.Text = spitArr[4].Split('=')[1];
  39. }
  40. catch { }
  41. }
  42. }
  43. private string Valid()
  44. {
  45. if (text_ip.Text.Trim() == string.Empty)
  46. {
  47. XtraMessageBox.Show(L.GetString("hostIsNotEmpty"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return null;
  48. }
  49. if (text_port.Text.Trim() == string.Empty)
  50. {
  51. XtraMessageBox.Show(L.GetString("portIsNotEmpty"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return null;
  52. }
  53. if (text_db.Text.Trim() == string.Empty)
  54. {
  55. XtraMessageBox.Show(L.GetString("databaseIsNotEmpty"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return null;
  56. }
  57. if (text_name.Text.Trim() == string.Empty)
  58. {
  59. XtraMessageBox.Show(L.GetString("usernameIsNotEmpty"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return null;
  60. }
  61. if (text_name.Text.Trim() == string.Empty)
  62. {
  63. XtraMessageBox.Show(L.GetString("pwdIsNotEmpty"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error); return null;
  64. }
  65. 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";
  66. }
  67. private void btn_ok_Click(object sender, EventArgs e)
  68. {
  69. var connString = Valid();
  70. IDbConnection conn = new MySqlConnection(connString);
  71. try
  72. {
  73. conn.Open();
  74. if (conn.State == ConnectionState.Open)
  75. {
  76. XtraMessageBox.Show(L.GetString("connectionSucceeded"), L.GetString("notification"), MessageBoxButtons.OK, MessageBoxIcon.Information);
  77. DialogResult = DialogResult.OK;
  78. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  79. if (string.IsNullOrEmpty(ConfigurationManager.ConnectionStrings["myconn"]?.ConnectionString))
  80. {
  81. config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings("myconn", connString));//增加
  82. }
  83. else
  84. {
  85. config.ConnectionStrings.ConnectionStrings["myconn"].ConnectionString = connString;//修改
  86. }
  87. config.Save(ConfigurationSaveMode.Modified);
  88. ConfigurationManager.RefreshSection("ConnectionStrings");
  89. Close();
  90. }
  91. else
  92. {
  93. XtraMessageBox.Show(L.GetString("failedConnection"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  94. return;
  95. }
  96. }
  97. catch
  98. {
  99. XtraMessageBox.Show(L.GetString("failedConnection"), L.GetString("error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
  100. return;
  101. }
  102. finally
  103. {
  104. conn?.Close();
  105. conn.Dispose();
  106. }
  107. }
  108. private void text_ip_Click(object sender, EventArgs e)
  109. {
  110. SoftKeyboard.ActiveSoftKeyboard(sender);
  111. }
  112. private void text_port_Click(object sender, EventArgs e)
  113. {
  114. SoftKeyboard.ActiveSoftKeyboard(sender);
  115. }
  116. private void text_db_Click(object sender, EventArgs e)
  117. {
  118. SoftKeyboard.ActiveSoftKeyboard(sender);
  119. }
  120. private void text_name_Click(object sender, EventArgs e)
  121. {
  122. SoftKeyboard.ActiveSoftKeyboard(sender);
  123. }
  124. private void text_pwd_Click(object sender, EventArgs e)
  125. {
  126. SoftKeyboard.ActiveSoftKeyboard(sender);
  127. }
  128. }
  129. }