getPassword.aspx.cs
上传用户:ah_jiwei
上传日期:2022-07-24
资源大小:54044k
文件大小:2k
源码类别:

数据库编程

开发平台:

Visual C++

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12. public partial class getPassword : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.     }
  17.     //注册按钮
  18.     protected void Button1_Click(object sender, EventArgs e)
  19.     {
  20.     }
  21.     //登陆按钮
  22.     protected void Button2_Click(object sender, EventArgs e)
  23.     {
  24.         string userId = TextBox1.Text.ToString().Trim();
  25.         string password = TextBox2.Text.ToString().Trim();
  26.         SqlConnection con = DB.connect_la();
  27.         try
  28.         {
  29.             con.Open();
  30.             string commandString = "select * from id_password_role where id = '" + userId + "' and password= '" + password + "'";
  31.             SqlCommand cmd = new SqlCommand(commandString, con);
  32.             SqlDataReader sdr = cmd.ExecuteReader();
  33.             if (sdr.Read())
  34.             {
  35.                 string role_log = sdr.GetString(2);
  36.                 Session["userId"] = userId;
  37.                 if (role_log.StartsWith("t"))
  38.                     Response.Redirect("teacher/index.aspx?");
  39.                 else if (role_log.StartsWith("s"))
  40.                     Response.Redirect("student/index.aspx?");
  41.                 else if (role_log.StartsWith("a"))
  42.                     Response.Redirect("administration/index.aspx?");
  43.             }
  44.             else
  45.             {
  46.                 Response.Write("<script>alert('用户名或密码错误!');window.location='login.aspx'</script>");
  47.             }
  48.             sdr.Close();
  49.             con.Close();
  50.         }
  51.         catch (Exception exception)
  52.         {
  53.             Response.Write("<script>alert('数据库读取异常!');window.location='login.aspx'</script>");
  54.         }
  55.         finally
  56.         {
  57.             if (con.State == ConnectionState.Open)
  58.             {
  59.                 con.Close();
  60.             }
  61.         }
  62.     }
  63. }