标题:C# 判断不同的用户登录,出现一个问题只能登录一个页面
只看楼主
白色的天
Rank: 2
等 级:论坛游民
帖 子:24
专家分:17
注 册:2013-11-9
结帖率:80%
已结贴  问题点数:10 回复次数:3 
C# 判断不同的用户登录,出现一个问题只能登录一个页面
我在做一个登录窗口,表中有两个用户,一个是管理员,一个是普通用户,我在表中用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("不好意思,该用户不存在");


                }

            }
搜索更多相关主题的帖子: 用户登录 private 用户名 管理员 密码 
2014-06-22 22:12
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:5 
打酱油路过,顺便说一句,哪有你这样写的,sql烂得不行啊
2014-06-22 22:42
asdfv63
Rank: 3Rank: 3
等 级:论坛游侠
威 望:5
帖 子:38
专家分:142
注 册:2013-4-15
得分:5 
red.read()应该写个while循环吧。
2014-06-23 08:06
白色的天
Rank: 2
等 级:论坛游民
帖 子:24
专家分:17
注 册:2013-11-9
得分:0 
问题解决了,是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("不好意思,该用户不存在");


                }

            }
2014-06-23 10:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-433248-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027972 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved