110,536
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netexamConnectionString"].ToString());
protected void login_Click(object sender, EventArgs e)
{
int type = Int32.Parse(ddlType.SelectedItem.Value);
string userid = txtuser.Text.Trim(); //保存登录页面输入的用户名
string inputpwd = txtpwd.Text.Trim(); //保存登录页面输入的密码
string pwd = "";
try
{
string sql = "select * from Users where ID like '" + userid + " '"+"and Role="+type;
SqlDataAdapter da = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && !ds.Tables[0].Rows[0].IsNull(0)) //判断是否有返回数据,若无,则说明不存在该用户
{
pwd = ds.Tables[0].Rows[0][2].ToString().Trim(); //获取密码
if (inputpwd == pwd) //密码正确登录成功
{
Session["ID"] = userid; //在session中记录登录用户ID
Session["Type"] = type; //在session中记录登录用户的用户类型
switch (type)
{
case 0: // 身份为学生时
Response.Redirect("../student/Stu_index.aspx"); //跳到学生主页
break;
case 1:
Response.Redirect("~/Web/Tch_index.aspx"); //跳到教师主页
break;
case 2:
Response.Redirect("~/Web/admin/Ad_index.aspx");
break;
}
}
else
{
Response.Write("<script>alert('密码错误,请检查后重新输入!')</script>");
}
}
else
{
Response.Write("<script language='javascript'>alert('该用户不存在或用户名输入错误,请检查后重新输入!')</script>");
}
}
catch (System.Exception ex)
{
Response.Write("<script>alert('错误原因:"+ex.Message.ToString()+"')</script>");
}
finally
{
}
}
}