private void btn_save_Click(object sender, EventArgs e) { iniFile.WriteValue("DBConfig", "DB Name", textBox_DBName.Text); iniFile.WriteValue("DBConfig", "DB Address", textBox_DBAddress.Text); iniFile.WriteValue("DBConfig", "User Name", textBox_userName.Text); iniFile.WriteValue("DBConfig", "Password", Algo.DESEncrypt(textBox_password.Text)); }
private void Form_DBConfig_Load(object sender, EventArgs e) { if (File.Exists(Form_Main.INIFILE_NAME)) { iniFile = new IniFile(Form_Main.INIFILE_NAME); textBox_DBAddress.Text = iniFile.ReadValue("DBConfig", "DB Address"); textBox_DBName.Text = iniFile.ReadValue("DBConfig", "DB Name"); textBox_userName.Text = iniFile.ReadValue("DBConfig", "User Name"); try { textBox_password.Text = Algo.DESDecrypt(iniFile.ReadValue("DBConfig", "Password")); } catch { textBox_password.Text = ""; } } }
/// <summary> /// 登录框 /// </summary> /// <param name="name">返回登录名</param> /// <param name="role">返回角色名</param> /// <returns>-1:取消,0:失败,1:成功</returns> public int LoginBox(out string name, out string role) { name = role = ""; Form LoginForm = new Form(); LoginForm.MinimizeBox = false; LoginForm.MaximizeBox = false; LoginForm.FormBorderStyle = FormBorderStyle.FixedDialog; LoginForm.StartPosition = FormStartPosition.CenterScreen; LoginForm.Width = 420; LoginForm.Height = 230; LoginForm.Text = "登录"; Label lbl_name = new Label(); lbl_name.Text = "登录名:"; lbl_name.Left = 92; lbl_name.Top = 44; lbl_name.Parent = LoginForm; lbl_name.AutoSize = true; TextBox textBox_name = new TextBox(); textBox_name.Left = 151; textBox_name.Top = 41; textBox_name.Width = 160; textBox_name.Parent = LoginForm; textBox_name.Text = ""; Label lbl_password = new Label(); lbl_password.Text = "密码:"; lbl_password.Left = 92; lbl_password.Top = 86; lbl_password.Parent = LoginForm; lbl_password.AutoSize = true; TextBox textBox_password = new TextBox(); textBox_password.Left = 151; textBox_password.Top = 83; textBox_password.Width = 160; textBox_password.Parent = LoginForm; textBox_password.Text = ""; textBox_password.PasswordChar = '*'; Button btn_DBConfig = new Button(); btn_DBConfig.Left = 80; btn_DBConfig.Top = 145; btn_DBConfig.Parent = LoginForm; btn_DBConfig.Text = "数据库连接设置"; btn_DBConfig.DialogResult = DialogResult.Yes; Button btn_login = new Button(); btn_login.Left = 165; btn_login.Top = 145; btn_login.Parent = LoginForm; btn_login.Text = "登录"; LoginForm.AcceptButton = btn_login;//回车响应 btn_login.DialogResult = DialogResult.OK; Button btn_cancel = new Button(); btn_cancel.Left = 251; btn_cancel.Top = 145; btn_cancel.Parent = LoginForm; btn_cancel.Text = "取消"; btn_cancel.DialogResult = DialogResult.Cancel; LoginForm.CancelButton = btn_cancel; try { switch (LoginForm.ShowDialog()) { case DialogResult.OK: // 读取连接配置文件 if (!File.Exists(INIFILE_NAME)) { MessageBox.Show("缺少数据库连接配置文件", "错误"); return(0); } iniFile = new IniFile(INIFILE_NAME); string DBAddress = iniFile.ReadValue("DBConfig", "DB Address"); string DBName = iniFile.ReadValue("DBConfig", "DB Name"); string userName = iniFile.ReadValue("DBConfig", "User Name"); try { string password = Algo.DESDecrypt(iniFile.ReadValue("DBConfig", "Password")); mDBM.Open($"User Id={userName};Password={password};Data Source={DBAddress}/{DBName};Min Pool Size=0; Connection Timeout=5"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); // 数据库连接设置 if (form_DBConfig == null || form_DBConfig.IsDisposed) { form_DBConfig = new Form_DBConfig(); } form_DBConfig.ShowDialog(); return(0); } string message; int index = mDBM.Login(textBox_name.Text, textBox_password.Text, out message); if (index == 1) { MessageBox.Show(message, "提示"); if (form_password == null || form_password.IsDisposed) { form_password = new Form_Password(); } if (form_password.ShowDialog() != DialogResult.OK) { return(-1); } } else if (index != 0) { if (-5 == index) //用户已登录 { if (DialogResult.Yes == MessageBox.Show(message + ", 是否强制注销", "登录失败", MessageBoxButtons.YesNo)) { mDBM.loginName = textBox_name.Text; mDBM.Logout(); } } else { MessageBox.Show(message, "登录失败"); } return(0); } else { MessageBox.Show(message, "提示"); } name = mDBM.loginName; role = mDBM.roleName; if (Form_Config.LoginTimeout > 0) { timer_op.Interval = Form_Config.LoginTimeout * 60 * 1000; timer_op.Start(); timer_start.Start(); } else { timer_op.Stop(); timer_start.Stop(); } return(1); case DialogResult.Yes: if (form_DBConfig == null || form_DBConfig.IsDisposed) { form_DBConfig = new Form_DBConfig(); } form_DBConfig.ShowDialog(); return(0); default: return(-1); } } finally { LoginForm.Close(); LoginForm.Dispose(); } }