小弟刚学.net,请大家帮注释一下!
小弟刚学.Net,学着坐了一个登陆的页面。。
。。现在有部分代码的意思不太明白,,请各位高手帮注解一下!!谢谢了。。。
------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb; //下面要用到数据库,所以要先加一个包含访问数据的命名空间!
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.Page.IsPostBack) //第一次载入页面是执行。
{
TextUser.Text = ""; //文本框TextUser的Text属性清空。
TextPass.Text = ""; //文本框TextPass的Text属性清空。
RequiredFieldValidator1.Text = ""; //同上,是验证的那个ID,清空内容!
RequiredFieldValidator2.Text = ""; //同上,是验证的那个ID,清空内容!
}
}
protected void ButLogin_Click(object sender, EventArgs e)
{
RequiredFieldValidator1.Text = ""; //同上,是验证的那个ID,清空内容!
RequiredFieldValidator2.Text = ""; //同上,是验证的那个ID,清空内容!
string User; //定义一个字符串变量,名为:User
string Pass; //定义一个字符串变量,名为:Pass
User = this.TextUser.Text; //把TextUser文本框中的内容赋值给刚才定义的User!
Pass = this.TextPass.Text; //把TextPass文本框中的内容赋值给刚才定义的Pass!
String LoginConn; //先定义一个“LoginConn”的变量,也就是Web.conFig里的“<add key="LoginConn" value="DataBase\GuessBook.accdb"/>”Key的值。
LoginConn = ConfigurationManager.AppSettings["LoginConn"]; //这里给LoginConn赋值,值就是Web.config里的调用。至于为什么这样赋值,我也不知道。本来是:configurationsetting.appsettings,但是会显示过时,上网查了一下改为:ConfigurationManager.AppSettings 后正常!不知道为什么!
OleDbConnection MyConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath(LoginConn)); //这里就是用上面定义的变量连接数据库了,和普通的连接Access查不多,只不过是把原来应该写数据库路径的地方,改成调用刚才定义的变量了。
MyConn.Open(); //打开数据集!
OleDbCommand cmd = MyConn.CreateCommand(); //这个暂不明白是什么意思。。。。
cmd.CommandText = "select * from 管理员"; //查询语句!查询“管理员”表!
cmd.CommandType = CommandType.Text; //这个暂不明白是什么意思。。。。
OleDbDataReader reader = cmd.ExecuteReader(); //这个暂不明白是什么意思。。。。
while (reader.Read()) //WHILE循环语句。
{
if (User == reader["用户"].ToString()) //如果,User=读取到的"用户"字段的值。这里这个"用户"字段要转换成字符串。。也就是后面跟的.ToString()
{
if (Pass == reader["密码"].ToString()) //同上,如果,Pass=读取到的"密码"字段的值。这里这个"密码"字段要转换成字符串。。也就是后面跟的.ToString()
{
Response.Redirect("Admin/Default.aspx"); //接上面:如果Pass=读取到的"密码"字段的值。则,重定向到"Admin/Default.aspx"页面,也就是说登陆成功了!
return; //不知道这个是干什么的。。。
}
else //否则的话。。也就是如果Pass不等于读取到的"密码"字段的值的话。。。
{
Response.Write("<script language=javascript>window.alert('密码不正确,请检查!');</script>"); //就用“Response.Write”显示"<script language=javascript>window.alert('密码不正确,请检查!');</script>",而这个Script的意思是弹出一个消息框!
return; //同上,不知道为什么要写他。。。
}
}
else //接上面第一个,如果:User不等于读取到的"用户"字段的值的话。。。
{
Response.Write("<script language=javascript>window.alert('帐号不正确,请检查!');</script>"); ////就用“Response.Write”显示"<script language=javascript>window.alert('帐号不正确,请检查!');</script>",而这个Script的意思是弹出一个消息框!
return; //同上,不知道为什么要写他。。。
}
}
}
protected void ButBack_Click(object sender, EventArgs e)
{
Response.Redirect("default.aspx"); //重定向到default.aspx页面。。就是点击返回按钮的作用。
}
protected void TextUser_TextChanged(object sender, EventArgs e)
{
}
protected void TextPass_TextChanged(object sender, EventArgs e)
{
}
}
请帮忙注解下上面红色代码的地方,,谢谢了,,尽量要说的通俗易懂。。。小弟新手,,不太明白。。。