private void Connect(object o) { if (mycon != null) { if (mycon.State != ConnectionState.Closed) { try { mycon.Close(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); return; } } string connectionString = string.Format("server={0};port={1};uid={2};pwd={3};DataBase={4}", ServerName, ServerPort, LoginName, LoginPassword, DatabaseName); try { mycon.ConnectionString = connectionString; mycon.Open(); MySqlCommand queryDataBaseCommand = new MySqlCommand("select * from user", mycon); DataSet ds = new DataSet(); MySqlDataReader reader = queryDataBaseCommand.ExecuteReader(); Users.Clear(); while (reader.Read()) { int index = reader.GetInt32("user_id"); string strName = reader.GetString("user_name"); string strPassword = reader.GetString("user_password"); User user = new User() { 序列号 = index, 名字 = strName, 密码 = strPassword }; user.changePassword += ChangePassword; Users.Add(user); } mycon.Close(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } }
private void ChangePassword(User user) { if (mycon != null) { if (mycon.State != ConnectionState.Closed) { mycon.Close(); } try { mycon.Open(); MySqlCommand queryDataBaseCommand = new MySqlCommand(string.Format("update user set user_password='******' where user_id={1}", MD5.GetMd5String(user.密码), user.序列号), mycon); queryDataBaseCommand.ExecuteNonQuery(); mycon.Close(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } }