private void Button_Click(object sender, RoutedEventArgs e) { if (txtPhone.Text.Trim() == "") { labPhoneMsg.Content = "请输入电话号码!"; txtPhone.Focus(); return; } else { //连接数据库对象 MySqlConnection conn = new MySqlConnection(Constant.myConnectionString); int sex; int type = 0; if (admin == false) { MessageBox.Show("无修改员工权限!"); } if (rdbMan.IsChecked == true) { sex = 1; } else { sex = 0; } if (rdbStuff.IsChecked == true) { type = 0; } else if (rdbEngineer.IsChecked == true) { type = 1; } else if (rdbExecutive.IsChecked == true) { type = 2; } else if (rdbManager.IsChecked == true) { type = 3; } string sql = string.Format("update user set user_name='{0}',sex='{1}',phone='{2}',type='{3}' where employee_id='{4}'", txtName.Text.Trim(), sex, txtPhone.Text.Trim(), type, user.employee_id); try { conn.Open();//打开通道,建立连接,可能出现异常,使用try catch语句 //对数据库进行插入 MySqlCommand cmd = new MySqlCommand(sql, conn); int result = cmd.ExecuteNonQuery(); if (result != 0) { MessageBox.Show("修改成功!"); up.getUserTable(1); } else { MessageBox.Show("修改失败!"); txtName.Text = ""; txtPhone.Text = ""; } } catch (MySqlException ex) { Console.WriteLine(ex.Message); MessageBox.Show("修改失败!"); txtName.Text = ""; txtPhone.Text = ""; } finally { conn.Close(); } } }
private void Button_Regist(object sender, RoutedEventArgs e) { if (txtId.Text.Trim() == "") { labIdMsg.Content = "请输入ID卡号!"; txtId.Focus(); return; } else if (txtName.Text.Trim() == "") { labNameMsg.Content = "请输入员工姓名!"; txtName.Focus(); return; } else if (txtPhone.Text.Trim() == "") { labPhoneMsg.Content = "请输入电话号码!"; txtPhone.Focus(); return; } else { //连接数据库对象 MySqlConnection conn = new MySqlConnection(Constant.myConnectionString); string uuid = System.Guid.NewGuid().ToString("N"); int sex; int type = 0; if (txtSex.IsChecked == true) { sex = 1; } else { sex = 0; } if (rdbStuff.IsChecked == true) { type = 0; } else if (rdbEngineer.IsChecked == true) { type = 1; } else if (rdbExecutive.IsChecked == true) { type = 2; } else if (rdbManager.IsChecked == true) { type = 3; } string sql = string.Format("insert into user (employee_id,user_name,user_pwd,sex,phone,state,type) values('{0}','{1}',123,'{2}','{3}',0,'{4}')", txtId.Text.Trim(), txtName.Text.Trim(), sex, txtPhone.Text.Trim(), type); try { conn.Open();//打开通道,建立连接,可能出现异常,使用try catch语句 //对数据库进行插入 MySqlCommand cmd = new MySqlCommand(sql, conn); int result = cmd.ExecuteNonQuery(); if (result != 0) { MessageBox.Show("插入成功!"); up.getUserTable(1); } else { MessageBox.Show("插入失败!"); txtId.Text = ""; txtName.Text = ""; txtPhone.Text = ""; } } catch (MySqlException ex) { Console.WriteLine(ex.Message); MessageBox.Show("插入失败!"); txtId.Text = ""; txtName.Text = ""; txtPhone.Text = ""; } finally { conn.Close(); } } }