不要写死,要调用数据库里的用户名和密码
我是新手,绑定后,如何写代码
实现:user_name             user_pwd
aaaaaaa                 123456
bbbbbb                  111111
就是,在登陆时候,当用户名是aaaaaaa的时候,验证密码是123456 当填写用户bbbbbbbbb的时候,验证密码是11111111,就是这个意思,我试了很多遍,就是总是弹出我编写的“用户名或密码错误!”谢谢,对于windows应用程序还是asp.net我都没实现,谢谢各位前辈!

解决方案 »

  1.   

    给你段代码参考::SqlCommand mCMD;
    try
    {
    if(Conn.State==ConnectionState.Closed)
    {
    Conn.Open();
    }
    mCMD=new SqlCommand("SELECT username,password  FROM users WHERE username='"+tbxUsername.Text+"'and password='"+tbxPassword.Text+"'",Conn);
    SqlDataReader mDR=mCMD.ExecuteReader();
    mCMD.Dispose();
    if(mDR.HasRows)//查询到存在
       {
    mDR.Close();
    Session["username"]=tbxUsername.Text;//读的时候 username=Session["username"].ToString;
    Response.Redirect("realhome.aspx");//也可以Server.Transfer("realhome.aspx")
     }
    else
    {
    Response.Write("<script language=\"javascript\">alert(\"输入的用户名或密码有错!!\");</script>"); }
    }
    catch(System.Data.SqlClient.SqlException err)
    {
    Conn.Close();
    // mCMD.Close();
    throw new Exception("打开数据库出错"+err);
    }
      

  2.   

    string strSQL="select user_name from table1 where user_name='"textbox1.text+"' and user_pwd='"+textbox2.text+"'";执行该sql语句,判断返回是否为空,不为空则说明验证成功..当然,这只是说了道理,要实用的话,要复杂..
      

  3.   

    string strSQL="select user_name from table1 where user_name='"textbox1.text+"' and user_pwd='"+textbox2.text+"'";
    千萬別用這種方法,這樣只要知道用戶名的人都可以登陸,想想看,如果在第二個textbox中填入"--"會有什么樣的后果?
      

  4.   

    錯了,在第一個textbox後面加上"--"
      

  5.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    string  strSQL="select Login_pwd from L_login where Login_name=''textBox1.Text''";
    if (textBox2.Text==strSQL)
    {
    MessageBox.Show("ok!");
    }
    else
    {
    MessageBox.Show("error");
    }
     
    }结果是:
    textbox2=select Login_pwd from L_login where Login_name=''textBox1.Text''
    才显示ok
      

  6.   

    各位高手,前辈,究竟怎么弄啊?
    数据库是hessam windows 集成验证
    数据库的L_login表
    Login_name Login_pwd
    一个textbox1,一个textbox2,一个button按钮,最简单的登陆框,高手们给点帮助啊!
      

  7.   

    string conn="Server=;DataBase=;Uid=;Pwd=";
    try
    {
    SqlConnection myConn = new SqlConnection(conn);
    myConn.Open();
    SqlCommand sqlCmd = new SqlCommand("select * from au_info where Login_name = '"+userName.Text+"'",myConn); 
    SqlDataReader dataRead = sqlCmd.ExecuteReader();
    if(dataRead.Read()) 
    {
    if(dataRead["Login_pwd"].ToString().CompareTo(userPass.Text == 0)
    {
    dataRead.Close();
    myConn.Close();
    return true;
    }
    else
    {
    userPass.Text = string.Empty;
    userPass.Focus();
    MessageBox.Show("输入的密码错误!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    return false;
    }
    }
    else
    {
    userName.Text=string.Empty;
    userPass.Text=string.Empty;
    userName.Focus();
    MessageBox.Show("没有这个用户!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
    return false;
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message+"读取数据错误,请检查数据库是否出错!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);

    Log.WriteErrorLog("发生登陆异常:"+ex.Message);
    return false;
    }
      

  8.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    if(getpwd()==true)
    {
    MessageBox.Show("登录成功!");
    frmMain f2=new frmMain();
    f2.Show();
    } else
    {
    MessageBox.Show("用户名或者密码错误!");
    this.textBox2.Text=null;
    }
    }

    private bool getpwd()
    {
    SqlConnection con=Data.con();
    con.Open();
    SqlCommand myCMD = new SqlCommand("select * from l_login where login_name='"+this.textBox1.Text.Trim()+"'and login_pwd='"+textBox2.Text.Trim()+"'",con); SqlDataReader myReader = myCMD.ExecuteReader(); if(myReader.Read())
    return true; else
    {
    return false;
    }