数据库连接实例

来源:互联网 发布:纽约时报 知乎 编辑:程序博客网 时间:2024/05/18 03:55
数据库连接实例

一、验证密码
  1.         if (TB_name.Text.Length < 6 || TB_name.Text.Length > 20)
  2.         {
  3.             LB_name .Text = "用户名长度不合法";
  4.             LB_name.ForeColor = Color.Red;
  5.             return ;
  6.         }
  7.         if (TB_password.Text == "")
  8.         {
  9.             LB_password.Text = "密码不能为空";
  10.             LB_password.ForeColor = Color.Red;
  11.             return ;
  12.         }
  13.         
  14.         try
  15.         {
  16.             SqlConnection conn = new SqlConnection(@"server=;database=NewsDB;Trusted_Connection=True");
  17.             string Sql = "SELECT user_password FROM tbuser WHERE user_name='" + TB_name.Text + "'";
  18.             SqlDataAdapter da = new SqlDataAdapter(Sql, conn);
  19.             DataSet ds = new DataSet();
  20.             da.Fill(ds);
  21.             DataTable dt = ds.Tables[0];
  22.             if (dt.Rows[0][0].ToString() ==TB_password .Text )
  23.             {
  24.                 Session["IsOkUserName"] = TB_name.Text;
  25.                 Response.Redirect("~/Default.aspx");
  26.                 return ;
  27.             }
  28.             else
  29.             {
  30.                 LB_password .Text  = "密码错误";
  31.                 return ;
  32.             }
  33.         }
  34.         catch
  35.         {
  36.             LB_name.Text = "用户不存在";
  37.             return ;
  38.         }
二、更改密码:
  1.  OleDbConnection mdbcnn1 = new OleDbConnection(@"  Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DSellManager.mdb;Persist Security Info=True");
  2.   string sSQL = "Update [password] Set [Password]='" + dbnewpw + "' where ID=1";
  3.   mdbcmd = new OleDbCommand("select * from  [password] where ID=1", mdbcnn1);
  4.   mdbcnn1.Open();
  5.   OleDbCommand myCommand = new OleDbCommand(sSQL, mdbcnn1);
  6.   if (myCommand.ExecuteNonQuery() > 0)
  7.   {
  8.       MessageBox.Show("修改密碼成功 ,請記好你的新密碼  ");
  9.       this.Close();
  10.   }
  11.   else
  12.   {
  13.       MessageBox.Show("修改密碼失敗!");
  14.   }
  15.   mdbcnn1.Close();