一个asp.net的小问题

来源:百度知道 编辑:UC知道 时间:2024/05/12 01:28:47
int flag = (int)myCommand.ExecuteScalar();//执行查询上面的sql语句,并返回结果集的第一行的第一列
myCommand.Connection.Close();//关闭sql语句的连接
if (flag > 0)//如果标记大于执行
{
Session["username"] = TextBox1.Text;
Response.Redirect("book.aspx");

}
else//如果标记=0时执行
{
//Label1.Visible=ture;
Label1.Text = "对不起!用户名和密码不符,请重新输入。";
TextBox1.Text = "";
TextBox2.Text = "";

}
如果把用户名设为0,flag不也是0吗?为什么还能进来?如果不是,请把int flag = (int)myCommand.ExecuteScalar();的具体意思详细说明,谢谢!
上面是
string strConn = "provider=Microsoft.jet.OLEDB.4.0;Data Source=" + Server.MapPath("database/book.mdb");
string strCmd = "select count(*) from book where (name like'" + TextBox1.Text + "' and password like'" + TextBox2.Text + "&

ExecuteScalar(),返回第一行第一列的数据这个是没错。

对于你说的:如果把用户名设为0,flag不也是0吗?为什么还能进来?

这点上要看你的SQL语句了。
比如:
SELECT [user_name] FROM [table]
这时,就是返回结果集的第一行第一列。如果[user_name]为0,那为就通过了。

作为用户登录不能这样判断。
str="SELECT [user_name] FROM [table] where user_name="+TextBox1.Text;
然后
SqlDataReader myReader1 = myCommand1.ExecuteReader();
if (myReader1.HasRows) //不包含一行或多行.
{
Session["username"] = TextBox1.Text;
Response.Redirect("book.aspx");
}
else
{
Label1.Text = "对不起!用户名和密码不符,请重新输入。";
TextBox1.Text = "";
TextBox2.Text = "";
}
哦。我知道了。
string strCmd = "select count(*) from book where (name like'" + TextBox1.Text + "' and password like'" + TextBox2.Text + "')"; 这句返回值就只有一行一列即你符合你的查询条件的用户个数。如果你用户名设为0的话,返回的值flag是1而不是0,因为有用户存在。