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