C#中实现密码的修改

 

密码修改事件:if(txtOldPassword.Text!="")
   {
    if(txtNewPassword.Text!="")
    {
     if(txtQNewPassword.Text!="")
     {
      if(txtQNewPassword.Text == txtNewPassword.Text)
      {
       int CID =2;
       PasswordLayer result = new PasswordLayer();
       if(result.CheckPassword(CID,txtOldPassword.Text.ToString())=="1")
       {
        result.UpdatePassword(CID,txtNewPassword.Text.ToString());
        MessageBox.Show("密码成功修改!");
        txtOldPassword.Text = "";
        txtNewPassword.Text = "";
        txtQNewPassword.Text = "";
       
       }
       else
       {
        MessageBox.Show("密码修改失败!");
        txtOldPassword.Focus();
       }
      }
      else
      {
       MessageBox.Show("密码输入不一致!");
       txtNewPassword.Text = "";
       txtQNewPassword.Text = "";
       txtNewPassword.Focus();
      }
     }
     else
     {
      MessageBox.Show("确认密码输入不能为空!");
      txtNewPassword.Text = "";
      txtNewPassword.Focus();
     }
    }
    else
    {
     MessageBox.Show("新密码不能为空!");
     txtNewPassword.Focus();
    }
   }
   else
   {
    MessageBox.Show("原密码不能为空");
    txtOldPassword.Focus();
   }

 

public string CheckOldPassword(int CID,string pwd)
  {
   SqlConnection conn = new SqlConnection(SqlConnConfig.ConnectionString);
   if(conn.State==ConnectionState.Closed)
   {
    conn.Close();
   }
    conn.Open();
    SqlDataAdapter adapter = new SqlDataAdapter("CheckPassword",conn);
    adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
    adapter.SelectCommand.Parameters.Add(new SqlParameter("@CID",SqlDbType.Int,4));
    adapter.SelectCommand.Parameters.Add(new SqlParameter("@Password",SqlDbType.NVarChar,50));
    adapter.SelectCommand.Parameters["@CID"].Value= CID;
    adapter.SelectCommand.Parameters["@Password"].Value = pwd;
    DataSet ds = new DataSet();
    adapter.Fill(ds);
    if(ds.Tables[0].Rows.Count == 1)
    {
     conn.Close();
     return "1";
    }
    else
    {
     conn.Close();
     return "0";
    }
 
  }

 

// 修改原密码,并存入到数据库中
  public bool UpdatePassword(int cID,string Password)
  {
   SqlConnection conn = new SqlConnection(SqlConnConfig.ConnectionString);
   conn.Open();
   SqlCommand cmd = new SqlCommand("UpdatePassword",conn);
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.Add(new SqlParameter("@CID",SqlDbType.Int,4));
   cmd.Parameters.Add(new SqlParameter("@Password",SqlDbType.NVarChar,50));

   cmd.Parameters["@CID"].Value = cID;
   cmd.Parameters["@Password"].Value = Password;
   try
   {
    cmd.ExecuteNonQuery();
    return true;
   }
   catch(Exception error)
   {
    return false;
    throw new Exception(error.Message,error);
    
   }
   finally
   {
    conn.Close();
   }
   
  }
 }

你可能感兴趣的:(C#中实现密码的修改)