我做一个基于C/S的小型系统,数据库有一张user表,其中有用户名和密码等相关消息,请问用什么简短的语句判断登陆的用户名和密码是否正确并登陆主界面 ?希望能够详细一点,谢谢!

解决方案 »

  1.   


                string sql = "select * from user where userName='"+txtUserName.Text.Trim()+"' and Pwd='"+txtPwd.Text+"'";
                try
                {
                    SqlCommand command = new SqlCommand(sql, "数据库连接connection,楼主自己写");
                    connection.Open();//楼主自己创建connection对象
                    if(command.ExecuteScalar()==null)
                    {
                        用户名或密码错误
                    }
                    else
                    {
                        登录主界面
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "数据库操作-登录");
                }
                finally
                {
                    connection.Close();
                }
            }
    在数据库中用按用户名和密码查找,找到记录就说明这个用户名和密码是正确的
    string sql = "select * from user where userName='"+txtUserName.Text.Trim()+"' and Pwd='"+txtPwd.Text+"'";
      

  2.   


    private void btnLogin_Click(object sender, EventArgs e)
            {
                try
                {
                    if (txtUserName.Text == "" || txtUserPwd.Text == "")
                    {
                        MessageBox.Show("用户名或密码不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        string name = txtUserName.Text.Trim();
                        string pwd = txtUserPwd.Text.Trim();
                        string ConnectionString = "server=localhost;database=MYdb;User=sa;Pwd=sa";
                        SqlConnection conn = new SqlConnection(ConnectionString);
                        conn.Open();
                        SqlCommand cmd = new SqlCommand("select * from tb_User where UserName='" + name + "' and UserPwd='" + pwd + "'", conn);
                        SqlDataReader sdr = cmd.ExecuteReader();
                        sdr.Read();
                        if (sdr.HasRows)
                        {
                            cmd.ExecuteNonQuery();
                            conn.Close();
                            this.Hide();
                            frmMain Main = new frmMain();
                            Main.Show();
                        }
                        else
                        {
                            txtUserName.Text = "";
                            txtUserPwd.Text = "";
                            MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
      

  3.   

    比较简单的办法呢,就是利用你输入的用户名跟密码,作为条件来查询用户表,如果查到有记录,就登录成功,登录是否成功可以用一个BOOL变量来作标记。
           //登入功能
            private void button2_Click(object sender, EventArgs e)
            {
                if (comboBox1.Text == "" || textBox1.Text == "")
                {
                    MessageBox.Show("请输入完整用户名和密码", "操作提示", MessageBoxButtons.OK,  MessageBoxIcon.Warning);
                    return;
                }            DataClass.SQLConn.Close();
                SqlCommand seleltUserinfocomm = new SqlCommand("select * from UserTable where UserID=@USERID and UPassWord=@PASSWORD",DataClass.SQLConn);
                seleltUserinfocomm.Parameters.Add(new SqlParameter("@USERID", SqlDbType.VarChar, 10));
                seleltUserinfocomm.Parameters["@USERID"].Value = comboBox1.Text;
                seleltUserinfocomm.Parameters.Add(new SqlParameter("@PASSWORD", SqlDbType.VarChar, 10));
                seleltUserinfocomm.Parameters["@PASSWORD"].Value = textBox1.Text;
                DataClass.SQLConn.Open();
                SqlDataReader myUinfoRe = seleltUserinfocomm.ExecuteReader();
                if (myUinfoRe.Read())
                {
                    this.Hide();
                    ID = myUinfoRe.GetString(0).ToString();
                    Password = myUinfoRe.GetString(1).ToString();
                    role = (bool)myUinfoRe.GetSqlBoolean(2);
                    if (role == true)
                    {
                        Role = "管理员";
                    }
                    else
                    {
                        Role = "普通用户";
                    }
                    UName = myUinfoRe.GetString(3).ToString();
                    login = true;
                    //MainForm mainform = new MainForm();
                    //mainform.ShowDialog();
                    
                }
                else
                {
                    MessageBox.Show("用户名字或者密码错误");
                }
                    DataClass.SQLConn.Close();
                    myUinfoRe.Close();
            }
      

  4.   

    简单一点,select * from usertable where username='"+userName+"' ";获取用户对像if(userObject!=null && userObject.Pwd==输入的用户密码)
    {
    登录成功,存入session
    }
      

  5.   

    用用户名 和密码为条件  查询 id  ok
      

  6.   

    都说了。
    先判断用户名,再比较密码。
       string ConnectionString = "";
                        SqlConnection conn = new SqlConnection(ConnectionString);
                        conn.Open();
                        SqlCommand cmd = new SqlCommand("select * from table where UserName='" + name + "' ", conn);
                        SqlDataReader dr = cmd.ExecuteReader();
                        if (dr.Read())
                        {
                           if(dr[""].ToString()==密码)
                           {
                           conn.Close();
                            Main.Show();
                           }
                        }
                        else
                        {
                            MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
      

  7.   

    好歹用户登陆还是要用sql参数安全。