用户和密码认证的问题。将c#程序中textbox控件的两个text的值,与sql sever中,consumer数据库的user1表中username列,code列进行比较.怎么写?

解决方案 »

  1.   

    。。这个是基础中的基础。网上随便google一下一大把
      

  2.   

    SqlConnection con= new SqlConnection(连接串);//连接串从web.Config读取
    SqlCommand cmd=new SqlCommand("select * from user1 where username=@u1 and code=@u2",con);
    cmd.Parameters.AddWithValue("@u1",textBox1.Text);
    cmd.Parameters.AddWithValue("@u2",textBox2.Text);
    con.Open();
    SqlDataReader read =cmd.ExecuteReader();
    if(read.Read())
    {
      MessageBox.Show("存在");}
    else
    {
      MessageBox.Show("不存在");}  
    con.Close();
      

  3.   

    这应该通过SQL语句从库中把username这列读出来,跟文本框中的值做比较
      

  4.   

    可以实现的方法太多了。如果有加密的话,可以选择在DB Server中建立一个存储过程专门处理这个。
      

  5.   

    用户和密码认证?不就是一条SQL语句的问题吗select * from user1 where username =textbox1.text  and code=textbox2.text执行后得到结果集中行数大于1验证通过
      

  6.   

     con.Open();
              SqlCommand com = new SqlCommand();
              com.CommandText = "Select * from Users where UserName='" + tbUserName.Text + "'";
              com.CommandType = CommandType.Text;
              com.Connection = con;
        SqlDataReader reader = com.ExecuteReader();
             if (reader.Read())
            {
                 string Password = reader["UserPassword"].ToString();
                 string strPassword = tbUserPassword.Text;
                 if (Password == strPassword)
                {
                      Session["uid"] = UserID;
                      Session["name"] =tbUserName.Text;
                      Response.Redirect("index.aspx");
                  }
                 else
                {
                     Response.Redirect("login.aspx");
                 }
              }
             else
            {
                  Response.Redirect("login.aspx");
              }
      

  7.   

    CSDN 论坛现在都是一些什么人啊即使是新手也问些有深度的问题吧~~~
      

  8.   

               如下代码语法上是否有问题?密码为空
             SqlConnection con = new SqlConnection("Server=localhots;uid=THEBESTMAN/fengxiang;pwd=;database=consumer");          SqlCommand cmd=new SqlCommand("select * from user1 where username=@u1 and code=@u2",con); 
              cmd.Parameters.AddWithValue("@u1",textBox1.Text); 
              cmd.Parameters.AddWithValue("@u2",textBox2.Text); 
              con.Open(); 
              SqlDataReader read =cmd.ExecuteReader();
              if (read.Read())
              {
                  login();
              }          else
              {
                  label3.Text = "用户名或者密码输入有误";
              }  
      

  9.   


    string str=string.Format("select * from user1 where username={0} and code={1}",textBox.Text,textBox.Text);
    //先创建一个sqlconnection类的对象sqlcon
    Sqlcommand sqlcom=sqlcon.CreateCommand();
    sqlcom.CommandType=CommandType.Text;
    sqlcom.CommandText=str.ToString();
    try
    {
       sqlcon.Open();
       if(sqlcom.ExecuteNonQuery()==-1)
       {
          Console.WriteLine("用户密码正确");
       }
    }
    catch
    {}
    sqlcon.Close();
    sqlcon.Dispose();
      

  10.   

    gooooooogle!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      

  11.   

    这就是简单的连下数据库后,再用个IF ELSE判断一下,就OK啦!
      

  12.   

    SqlConnection con= new SqlConnection(连接串);
    SqlCommand cmd=new SqlCommand("select * from user1 where username=@u1 and code=@u2",con); 
    cmd.Parameters.AddWithValue("@u1",textBox1.Text); 
    cmd.Parameters.AddWithValue("@u2",textBox2.Text); 
    con.Open(); 
    SqlDataReader read =cmd.ExecuteReader(); 
    if(read.Read()) 

      MessageBox.Show("存在"); } 
    else 

      MessageBox.Show("不存在"); }  
    con.Close();
      

  13.   

    SqlConnection con= new SqlConnection(连接串);
    SqlCommand cmd=new SqlCommand("select * from user1 where username=@u1 and code=@u2",con);
    cmd.Parameters.AddWithValue("@u1",textBox1.Text);
    cmd.Parameters.AddWithValue("@u2",textBox2.Text);
    con.Open();
    SqlDataReader read =cmd.ExecuteReader();
    if(read.Read())
    {
      MessageBox.Show("存在");}
    else
    {
      MessageBox.Show("不存在");} 
    con.Close();用參數是對的,如果不怕'出現錯誤的話.
      

  14.   

       LZ你去确实是来送分的...LS的那些答案都可以实现你的要求....
      

  15.   

    这个最好在数据库上建个存储过程,这样更安全点:con.Open(); 
              SqlCommand com = new SqlCommand(); 
              com.CommandText = "Select * from Users where UserName='" + tbUserName.Text + "'"; 
              com.CommandType = CommandType.Text; 
              com.Connection = con; 
        SqlDataReader reader = com.ExecuteReader(); 
            if (reader.Read()) 
            { 
                string Password = reader["UserPassword"].ToString(); 
                string strPassword = tbUserPassword.Text; 
                if (Password == strPassword) 
                { 
                      Session["uid"] = UserID; 
                      Session["name"] =tbUserName.Text; 
                      Response.Redirect("index.aspx"); 
                  } 
                else 
                { 
                    Response.Redirect("login.aspx"); 
                } 
              } 
            else 
            { 
                  Response.Redirect("login.aspx"); 
              } 
    简单写了下,不知道有帮助没有,呵呵
      

  16.   

    取得text中的值,放入sql语句中,然后与数据库相应字段比较。
      

  17.   


    CREATE PROCEDURE UserLogin @UserID varchar(10) , @Password varchar(10), @OptDescription varchar(50) output AS select UserID from Users where UserID = @UserID if (@@RowCount<1) begin ----1  set @OptDescription ='noUser' set @UserID=0 end------1  else begin ----2   SELECT UserID FROM Users WHERE (UserID = @UserID) AND (Password = @Password ) if (@@RowCount<1) begin -----3  set @OptDescription ='UserError' set @UserID=0 end-------3  else begin -----4  set @OptDescription='access' end ------4   end------2  RETURN GO 
      

  18.   

    用户和密码认证?不就是一条SQL语句的问题吗 select * from user1 where username =textbox1.text  and code=textbox2.text 执行后得到结果集中行数大于1验证通过来接分的
      

  19.   

    用户和密码认证?不就是一条SQL语句的问题吗 select * from user1 where username =textbox1.text  and code=textbox2.text 执行后得到结果集中行数大于1验证通过 来接分的这样很容易造成注入式攻击,别人在用户名输入 'or 1=1 保证让正常进入你的系统 
      

  20.   

    谢谢大家的支持,我sql没配置好,所以连不上,不是语法的问题!谢了!
      

  21.   

    谢大家的支持,我sql没配置好,所以连不上,不是语法的问题!谢了!