注册 登录
编程论坛 C# 论坛

C# 判断不同的用户登录,出现一个问题只能登录一个页面

白色的天 发布于 2014-06-22 22:12, 679 次点击
我在做一个登录窗口,表中有两个用户,一个是管理员,一个是普通用户,我在表中用int型来表示他们的区别,0是管理员的权限,1是普通用户,问题是表中第一个管理员可以登录,第二个普通用户不能登录,请大神指点指点......
private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" && textBox2.Text == "")
            {
                MessageBox.Show("请输入用户名或密码,不能为空");
            }
            else
            {
                if (textBox1.Text != "" && textBox2.Text != "")
                {
                    string str = ConfigurationManager.ConnectionStrings["kecheng"].ConnectionString;
                    SqlConnection con = new SqlConnection(str);
                    con.Open();
                    string strsql = "SELECT * FROM userssss";
                    SqlCommand com = new SqlCommand(strsql, con);
                    SqlDataReader red = com.ExecuteReader();

                    if (red.Read())
                    {
                        string name = red["username"].ToString();
                        string pad = red["passwd"].ToString();
                        int i = Convert.ToInt32(red["userpowsd"]);
                        if (textBox1.Text == name && textBox2.Text == pad)
                        {

                            if (i == 0)
                            {
                                admin f = new admin();
                                f.Show();

                            }
                            else
                            {

                                kaosheng kao = new kaosheng();
                                kao.Show();
                            }
                        }
                        else
                        {
                            MessageBox.Show("不存在该用户");
                        
                        }


                    }
                    con.Close();


                }
                else
                {
                    MessageBox.Show("不好意思,该用户不存在");


                }

            }
只有本站会员才能查看附件,请 登录
3 回复
#2
yhlvht2014-06-22 22:42
打酱油路过,顺便说一句,哪有你这样写的,sql烂得不行啊
#3
asdfv632014-06-23 08:06
red.read()应该写个while循环吧。
#4
白色的天2014-06-23 10:03
问题解决了,是sql语句问题,谢谢各位先
 if (textBox1.Text == "" && textBox2.Text == "")
            {
                MessageBox.Show("请输入用户名或密码,不能为空");
            }
            else
            {
                if (textBox1.Text != "" && textBox2.Text != "")
                {
                    string str = ConfigurationManager.ConnectionStrings["kecheng"].ConnectionString;
                    SqlConnection con = new SqlConnection(str);
                    con.Open();
                    string strsql = "SELECT * FROM userssss where username='"+textBox1.Text+"' and passwd='"+textBox2.Text+"'";
                    SqlCommand com = new SqlCommand(strsql, con);
                    SqlDataReader red = com.ExecuteReader();

                 if(red.Read())
                    {
                        int i =Convert.ToInt32(red["userpowsd"]);
                       

                            if(i == 0)
                            {
                             
                              
                                
                                admin f = new admin();
                                f.Text = red["username"].ToString();
                                f.Show();

                            }
                            else
                            {

                                kaosheng kao = new kaosheng();
                             kao.Text = red["username"].ToString();
                                kao.Show();
                            }
                        
                     
                     

                    }
                    con.Close();


                }
                else
                {
                    MessageBox.Show("不好意思,该用户不存在");


                }

            }
1