Example #1
0
 private void ButReject_Click(object sender, EventArgs e)
 {
     try
     {
         if (RegistName.Text.Trim().Equals(""))
         {
             MessageBox.Show("用户名不能为空!", "提示");
             RegistName.Focus();
         }
         else if (PassWord.Text.Trim().Equals(""))
         {
             MessageBox.Show("密码不能为空!", "提示");
             PassWord.Focus();
         }
         else if (UserName(RegistName.Text.Trim()))
         {
             MessageBox.Show("用户名已经存在!", "提示");
             ClearAll();
         }
         else
         {
             string sqlStr = "insert user_info (userName, userPwd, roles) values('" + RegistName.Text.Trim() + "','" + PassWord.Text.Trim() + "','" + RolesBox.Text.Trim() + "')";
             if (DBconn.PDData(sqlStr))
             {
                 MessageBox.Show("用户" + RegistName.Text.Trim() + "注册成功!");
             }
             this.Close();
         }
     }catch (Exception ex)            {
         DBconn.conn.Close();
         MessageBox.Show(ex.Message);
         ClearAll();
     }
 }
        // 修改按钮
        private void userGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                // 验证选择行
                if (userGridView.Columns[e.ColumnIndex].Name == "Modify" && e.ColumnIndex >= 0)
                {
                    // 获取需要修改的用户信息
                    string userId    = userGridView.CurrentRow.Cells[0].Value.ToString().Trim();
                    string userName  = userGridView.CurrentRow.Cells[1].Value.ToString().Trim();
                    string userPwd   = userGridView.CurrentRow.Cells[2].Value.ToString().Trim();
                    string userRoles = userGridView[3, e.RowIndex].EditedFormattedValue.ToString();

                    if (userName.Equals(""))
                    {
                        MessageBox.Show("用户名不能为空!");
                        return;
                    }
                    else if (userPwd.Equals(""))
                    {
                        MessageBox.Show("用户密码不能为空!");
                        return;
                    }
                    else if (userRoles.Equals(""))
                    {
                        MessageBox.Show("用户角色不能为空!");
                        return;
                    }
                    else
                    {
                        // 在数据库中修改
                        string sql = "update user_info set userName = '******', userPwd = '" + userPwd + "', roles = '" + userRoles + "' where userId = '" + int.Parse(userId) + "'";

                        //DataSet dataSet = DBconn.upData(sql);

                        if (DBconn.PDData(sql))
                        {
                            MessageBox.Show("用户:" + userName + " 修改成功");
                        }
                    }
                }
            }catch (Exception ex)
            {
                MessageBox.Show("修改失败:" + ex.Message);
            }
            finally
            {
                // 关闭数据库连接
                DBconn.conn.Close();
            }
        }