您的位置:首页 > 数据库

数据库连接实例

2008-11-04 16:39 148 查看
数据库连接实例
一、验证密码        if (TB_name.Text.Length < 6 || TB_name.Text.Length > 20)
        {
            LB_name .Text = "用户名长度不合法";
            LB_name.ForeColor = Color.Red;
            return ;
        }
        if (TB_password.Text == "")
        {
            LB_password.Text = "密码不能为空";
            LB_password.ForeColor = Color.Red;
            return ;
        }
        
        try
        {
            SqlConnection conn = new SqlConnection(@"server=;database=NewsDB;Trusted_Connection=True");
            string Sql = "SELECT user_password FROM tbuser WHERE user_name='" + TB_name.Text + "'";
            SqlDataAdapter da = new SqlDataAdapter(Sql, conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
            if (dt.Rows[0][0].ToString() ==TB_password .Text )
            {
                Session["IsOkUserName"] = TB_name.Text;
                Response.Redirect("~/Default.aspx");
                return ;
            }
            else
            {
                LB_password .Text  = "密码错误";
                return ;
            }
        }
        catch
        {
            LB_name.Text = "用户不存在";
            return ;
        }
二、更改密码: OleDbConnection mdbcnn1 = new OleDbConnection(@"  Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DSellManager.mdb;Persist Security Info=True");
  string sSQL = "Update [password] Set [Password]='" + dbnewpw + "' where ID=1";
  mdbcmd = new OleDbCommand("select * from  [password] where ID=1", mdbcnn1);
  mdbcnn1.Open();
  OleDbCommand myCommand = new OleDbCommand(sSQL, mdbcnn1);
  if (myCommand.ExecuteNonQuery() > 0)
  {
      MessageBox.Show("修改密碼成功 ,請記好你的新密碼  ");
      this.Close();
  }
  else
  {
      MessageBox.Show("修改密碼失敗!");
  }
  mdbcnn1.Close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: