一.设置窗口

1.主要包含设置服务器、用户、密码等
image.png
这个是子窗体
即设置服务器连接数据库
构造函数里写(Form form1)
子窗体功能;
1.读取ini文件的数据库设置
2.用户根据自己的需求改变设置
3.保存设置进ini文件
4.改变主窗体的SqlConnection—-此处详见
语雀内容

二、完整的子窗体代码

  1. public partial class SqlConnector : DevExpress.XtraEditors.XtraForm
  2. {
  3. private string filepath = "";
  4. public SqlConnector()
  5. {
  6. InitializeComponent();
  7. filepath = Form1.GetDirectoryInfo(3) + "Log\\SqlConfig.ini";
  8. this.txt_Server.Text = INIHelper.Read("SqlConnector", "server", "127.0.0.1", filepath);
  9. this.txt_UseID.Text = INIHelper.Read("SqlConnector", "userID", "root", filepath);
  10. this.txt_Port.Text = INIHelper.Read("SqlConnector", "port", "3306", filepath);
  11. }
  12. public SqlConnector(Form1 form1)
  13. {
  14. InitializeComponent();
  15. filepath = Form1.GetDirectoryInfo(3) + "Log\\SqlConfig.ini";
  16. this.txt_Server.Text = INIHelper.Read("SqlConnector", "server", "127.0.0.1", filepath);
  17. this.txt_UseID.Text = INIHelper.Read("SqlConnector", "userID", "root", filepath);
  18. this.txt_Port.Text = INIHelper.Read("SqlConnector", "port", "3306", filepath);
  19. this.txt_Password.Text = INIHelper.Read("SqlConnector", "password", "mypassword", filepath);
  20. }
  21. //定义委托、事件
  22. public delegate void ChangeSqlConnection(string connStr);
  23. public event ChangeSqlConnection changeSqlConn;
  24. /// <summary>
  25. /// 关闭窗体时重写ini文件且设置父窗体的SqlConnector
  26. /// </summary>
  27. /// <param name="sender"></param>
  28. /// <param name="e"></param>
  29. private void SqlConnector_FormClosed(object sender, FormClosedEventArgs e)
  30. {
  31. if (!string.IsNullOrEmpty(filepath))
  32. {
  33. INIHelper.Write("SqlConnector", "server", txt_Server.Text.ToString(), filepath);
  34. INIHelper.Write("SqlConnector", "userID", txt_UseID.Text.ToString(), filepath);
  35. INIHelper.Write("SqlConnector", "port", txt_Port.Text.ToString(), filepath);
  36. INIHelper.Write("SqlConnector", "password", txt_Password.Text.ToString(), filepath);
  37. string connStr = String.Format("server={0};user id={1}; password={2}; port={3}; database=mysql; pooling=false; charset=utf8", txt_Server.Text.ToString().Trim(), txt_UseID.Text.ToString().Trim(), txt_Password.Text.ToString(),txt_Port.Text.ToString().Trim());
  38. changeSqlConn(connStr);
  39. }
  40. }
  41. }

三、INIHelper—-用来改写ini中某个节点下的key对应的value值

INI文件构造
[section]
key=value

  1. class INIHelper
  2. {
  3. /// <summary>
  4. /// 为INI文件中指定的节点取得字符串
  5. /// </summary>
  6. /// <param name="lpAppName">欲在其中查找关键字的节点名称</param>
  7. /// <param name="lpKeyName">欲获取的项名</param>
  8. /// <param name="lpDefault">指定的项没有找到时返回的默认值</param>
  9. /// <param name="lpReturnedString">指定一个字串缓冲区,长度至少为nSize</param>
  10. /// <param name="nSize">指定装载到lpReturnedString缓冲区的最大字符数量</param>
  11. /// <param name="lpFileName">INI文件完整路径</param>
  12. /// <returns>复制到lpReturnedString缓冲区的字节数量,其中不包括那些NULL中止字符</returns>
  13. [DllImport("kernel32")]
  14. private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
  15. /// <summary>
  16. /// 修改INI文件中内容
  17. /// </summary>
  18. /// <param name="lpApplicationName">欲在其中写入的节点名称</param>
  19. /// <param name="lpKeyName">欲设置的项名</param>
  20. /// <param name="lpString">要写入的新字符串</param>
  21. /// <param name="lpFileName">INI文件完整路径</param>
  22. /// <returns>非零表示成功,零表示失败</returns>
  23. [DllImport("kernel32")]
  24. private static extern int WritePrivateProfileString(string lpApplicationName, string lpKeyName, string lpString, string lpFileName);
  25. /// <summary>
  26. /// 读取INI文件值
  27. /// </summary>
  28. /// <param name="section">节点名</param>
  29. /// <param name="key">键</param>
  30. /// <param name="def">未取到值时返回的默认值</param>
  31. /// <param name="filePath">INI文件完整路径</param>
  32. /// <returns>读取的值</returns>
  33. public static string Read(string section, string key, string def, string filePath)
  34. {
  35. StringBuilder sb = new StringBuilder(1024);
  36. GetPrivateProfileString(section, key, def, sb, 1024, filePath);
  37. return sb.ToString();
  38. }
  39. /// <summary>
  40. /// 写INI文件值
  41. /// </summary>
  42. /// <param name="section">欲在其中写入的节点名称</param>
  43. /// <param name="key">欲设置的项名</param>
  44. /// <param name="value">要写入的新字符串</param>
  45. /// <param name="filePath">INI文件完整路径</param>
  46. /// <returns>非零表示成功,零表示失败</returns>
  47. public static int Write(string section, string key, string value, string filePath)
  48. {
  49. //CheckPath(filePath);
  50. return WritePrivateProfileString(section, key, value, filePath);
  51. }
  52. /// <summary>
  53. /// 删除节
  54. /// </summary>
  55. /// <param name="section">节点名</param>
  56. /// <param name="filePath">INI文件完整路径</param>
  57. /// <returns>非零表示成功,零表示失败</returns>
  58. public static int DeleteSection(string section, string filePath)
  59. {
  60. return Write(section, null, null, filePath);
  61. }
  62. /// <summary>
  63. /// 删除键的值
  64. /// </summary>
  65. /// <param name="section">节点名</param>
  66. /// <param name="key">键名</param>
  67. /// <param name="filePath">INI文件完整路径</param>
  68. /// <returns>非零表示成功,零表示失败</returns>
  69. public static int DeleteKey(string section, string key, string filePath)
  70. {
  71. return Write(section, key, null, filePath);
  72. }
  73. /// <summary>
  74. /// 检查文件是否存在
  75. /// </summary>
  76. /// <param name="filePath"></param>
  77. /// <returns></returns>
  78. private static void CheckPath(string filePath)
  79. {
  80. if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath))
  81. {
  82. throw new ArgumentNullException("filePath");
  83. }
  84. }
  85. }

四、主窗体中数据库代码

  1. #region 数据库操作
  2. public MySqlConnection conn;
  3. private DataTable data;
  4. private MySqlDataAdapter da;
  5. private MySqlCommandBuilder cb;
  6. private DataGrid dataGrid;
  7. private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
  8. {
  9. SqlConnector sql = new SqlConnector(this);
  10. sql.changeSqlConn += new SqlConnector.ChangeSqlConnection(SetSqlConnection);
  11. sql.Show(this);
  12. }
  13. /// <summary>
  14. /// 根据INI文件中的设置改变数据库的连接
  15. /// </summary>
  16. private void SetSqlConnection(string connStr)
  17. {
  18. if(conn!=null)
  19. {
  20. conn.Close();
  21. }
  22. try
  23. {
  24. conn = new MySqlConnection(connStr);
  25. conn.Open();
  26. XtraMessageBox.Show("连接数据库成功!");
  27. }
  28. catch(Exception ex)
  29. {
  30. XtraMessageBox.Show("Error connecting to the server:" + ex.Message);
  31. }
  32. }
  33. #endregion