飞石 发表于 2008-4-30 17:44

密码验证问题....连接数据库验证

我做了一个登陆页面,然后用它连接SQL数据库.目前情况是,用户名输入正确,密码输入错误.页面会判断并且转入error.aspx,但是用户名输入错误,则不管密码是否正确,判断都不会执行,只会停留在当前页面.
  我想要的结果是,密码输入错误,则跳转到一个页面提示密码错误.用户名输入错误也能判断,并且跳转到另一个页面,提示用户名错误..请问这个代码该怎么写?
  说一下我的数据库名字是style,表名users,有id,name,password三项.请问该怎么写才能实现我想要的功能?谢谢各位了!

[color=Red]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)
    {

    }
    protected void login_in_Click(object sender, EventArgs e)
    {
        string constring = "provider=sqloledb;server=AIRFLY;uid=sa;pwd=fly1+2=3;database=style";
        OleDbConnection con = new OleDbConnection(constring);
        string sql = "select * from users where name='" + name.Text.ToString() + "'";

        OleDbCommand com = new OleDbCommand(sql, con);
        com.Connection.Open();
        OleDbDataReader dr;
        dr = com.ExecuteReader();

        while (dr.Read())
        {


            if (dr[2].ToString() == password.Text)
            {
                Response.Redirect("index.aspx");
            }
            else
                Response.Redirect("error.aspx");
        }

    }
}
[/color]

shezhenhui1989 发表于 2008-4-30 17:46

count(*)

hebingbing 发表于 2008-4-30 18:09

while (dr.Read())
        {


            if (dr[2].ToString() == password.Text)
            {
                Response.Redirect("index.aspx");
            }
            else
                Response.Redirect("error.aspx");
        }
给这个加个if不就可以了啊……
if(dr.read())
{
}
else
{
转到用户不存在提示页面。
}

飞石 发表于 2008-4-30 20:45

2楼的朋友写的我不知道是什么意思!我是初学者,麻烦写详细的笨蛋教程那种。我才能看懂!谢谢了!
3楼的朋友你的意思是这样加吗?可是会发生错误!我是初学者,能不能把最终代码告诉我一下!谢谢了!
while (dr.Read())
        {


            if (dr[2].ToString() == password.Text)
            {
                Response.Redirect("index.aspx");
            }
            else
                Response.Redirect("error.aspx");
        }

if(dr.read())
{
}
else
{
转到用户不存在提示页面。
}

hxfly 发表于 2008-5-1 01:29

最笨的方法分两部判断。
别用户名和密码一起判断。
先查询用户是否存在,不存在直接跳到用户不存在的页面,如果用户存在,在判断这个用户的密码是不是和提交的密码相同,如果不同,跳到密码错误页。

飞石 发表于 2008-5-1 12:24

楼上的朋友能不能写一下代码?
我理解你的意思,但我写出来的代码总是错误。。

hxfly 发表于 2008-5-1 14:35

string constring = "provider=sqloledb;server=AIRFLY;uid=sa;pwd=fly1+2=3;database=style";

        OleDbConnection con = new OleDbConnection(constring);
        string sql = "select * from users where name='" + name.Text.ToString() + "'";
        DataSet ds = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter(sql, con);        
        da.Fill(ds);
        DataTable dt = ds.Tables[0];
        if(dt.Rows.Count <1)
        {
            Response.Write ("用户名错误!");
            Response .End ();
        }
        else
        {
            if (dt.Rows[0]["UserPwd"].ToString != password.Text)
            {
                Response.Write ("密码错误!");
                Response .End ();
            }
        }



不知道对不对呢,呵呵

beniao 发表于 2008-5-1 16:38

楼上说得很清楚了..路过看看..

页: [1]

编程论坛