//从配置文件中获取连接字符串
string connstr = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//创建连接对象
OleDbConnection conn = new OleDbConnection(connstr);
string sqlText = "select * from students where StuName='" + txtName.Text + "' and StuPwd='" + txtPwd.Text+"'";
//创建命令对象
OleDbCommand cmd = new OleDbCommand( sqlText, conn);
//打开数据库连接
conn.Open();
//执行命令对象中的sql命令,获取数据
//注意datareader的特点,只读,向前,每次一条记录。
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
lblMessage.Text = "登录成功";
Session["IsLogin"] = true;
Session["StudentID"]= dr["StuID"].ToString();
}
else lblMessage.Text = "登录失败";
dr.Close();
conn.Close();