注册 登录
编程论坛 ASP.NET技术论坛

新手求教(登陆时出错)

qjyzpsy 发布于 2014-07-23 20:04, 1686 次点击
我在做一个简单的注册登录界面,注册没问题。但是登录时我输了正确的用户名和密码,用户名检测没错(会提示存不存在)但是提示密码错误
相关代码如下:
注册:
 protected void Button2_Click(object sender, EventArgs e)
    {
        if (TextBox2.Text == TextBox3.Text)
        {
            string sqla = @"server=.;database=yezi;UID=sa;PWD="";//连接数据库的字串
            SqlConnection conn = new SqlConnection(sqla);//通过数据库的字串连接数据库
            SqlConnection conn1 = new SqlConnection(sqla);//通过数据库的字串连接数据库
            conn.Open();//打开连接
            string sqlinscmd = @"select * from yezi_users where username='" + TextBox1.Text + "'";//sql命令字串
            SqlCommand sqlcmd = new SqlCommand(sqlinscmd, conn);//让sql命令字串通过conn连接去执行
            SqlDataReader dr = sqlcmd.ExecuteReader();//把sql命令产生的行写入dr数组,一次一行
            if (dr.Read())//dr.Read()执行读取
            {
                Response.Write("<script>alert('用户存在')</script>");//提示
            }
            else
            {
                conn.Close();//关闭数据库连接
                conn.Open();
                string sqlinscmd1 = @"insert into yezi_users values(" + TextBox1.Text + "," + TextBox2.Text + ",0)";//sql命令字串
                SqlCommand sqlcmd1 = new SqlCommand(sqlinscmd1, conn);//让sql命令字串通过conn连接去执行
                sqlcmd1.ExecuteNonQuery();//执行sql命令
                conn.Close();//关闭数据库连接
                Response.Write("<script>alert('用户添加成功');window.location('Default.aspx')</script>");//提示
            }
        }
        else
        {
            Response.Write("<script>alert('两次密码不相同')</script>");//提示
        }
    }
登陆:
 protected void Button1_Click(object sender, EventArgs e)
    {
        string sqla = @"server=.;database=yezi;UID=sa;PWD="";//连接数据库的字串
        SqlConnection conn = new SqlConnection(sqla);//通过数据库的字串连接数据库
        conn.Open();//打开连接
        string sqlinscmd = @"select * from yezi_users where username='" + TextBox1.Text + "'";//sql命令字串
        SqlCommand sqlcmd = new SqlCommand(sqlinscmd, conn);//让sql命令字串通过conn连接去执行
        SqlDataReader dr = sqlcmd.ExecuteReader();//把sql命令产生的行写入dr数组,一次一行
        if (dr.Read())//dr.Read()执行读取
        {
            
            if (dr["userpwd"].ToString() == TextBox1.Text)
            {
                    Response.Write("<script>alert('登录成功');window.location='普通用户.aspx';</script>");//提示
            }
            else
            {
                 Response.Write("<script>alert('密码错误')</script>");//提示
             }
            
        }
        else
        {
             Response.Write("<script>alert('用户不存在')</script>");//提示
        }
        conn.Close();//关闭数据库连接
      
     }
数据库里我的userpwd类型为char[50],我注册输入密码为"123",但是dr["userpwd"].ToString().Length得到的结果为50
请大神指导一下
4 回复
#2
零無2014-07-24 00:09
dr["userpwd"].ToString().Length这个代码的意思是那个数据库字段的类型长度。要是找注册输入密码的长度,应该是textbox的对象操作,比如是t,那就是t.Length

你下面的这两个代码username字段和userpwd字段匹配的都是TextBox1.Text,即账号,密码匹配同一个文本框的内容,当然会不对啊。
   string sqlinscmd = @"select * from yezi_users where username='" + TextBox1.Text + "'";//sql命令字串

 if (dr["userpwd"].ToString() == TextBox1.Text)
            {
                    Response.Write("<script>alert('登录成功');window.location='普通用户.aspx';</script>");//提示
            }
#3
qjyzpsy2014-07-24 12:12
回复 2 楼 零無
可是我把TextBox1改成TextBox2(就是输密码的文本框)后,还是出错
#4
零無2014-07-25 20:22
回复 2 楼 零無
你确定是改的这个代码的文本框?
if (dr["userpwd"].ToString() == TextBox1.Text)
            {
                    Response.Write("<script>alert('登录成功');window.location='普通用户.aspx';</script>");//提示
            }
#5
Issac_abc2014-07-30 14:59
string sqlinscmd = @"select * from yezi_users where username='" + TextBox1.Text + "'";//sql命令字串  这里查询 是根据用户名查询的
if (dr["userpwd"].ToString() == TextBox1.Text) //这里判断 是用名给密码的判断,这里有问题 dr["userpwd"].ToString()应该是给密码来比较吧
1