标题:大半天了,我实在找不出究竟错在哪~正大汗-_-U
只看楼主
一刀客
Rank: 1
等 级:新手上路
帖 子:181
专家分:0
注 册:2007-10-26
 问题点数:0 回复次数:4 
大半天了,我实在找不出究竟错在哪~正大汗-_-U



验证管理员输入的帐号和密码是否正确
在“登录”按钮我写了如下的代码:
private void btnlogin_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtuser.Text) || String.IsNullOrEmpty(txtpwd.Text))
            { MessageBox.Show("输入帐号/密码再登录", "错误提示"); return; }
            int i, flag = 0;
            label1.Text = null;//密码下面有个lable1,用来显示帐号和密码。
            string str1 = txtuser.Text; string str2 = txtpwd.Text;
            string StrSel = "select * from [db_user]";
            string strRow1 = ""; string strRow2 = "";
            string ConnectStr = "server=localhost;database=ClassDesign;uid=sa;pwd=;";
            SqlConnection MyCon = new SqlConnection(ConnectStr);
            try
            {
                MyCon.Open();
                this.Text = "已连接,正在登录";
            }
            catch (SqlException e1)
            {
                MessageBox.Show("连接失败", "出错");
                return;
            }
            SqlCommand MyCmd = new SqlCommand(StrSel,MyCon);
            SqlDataAdapter MydataAdapter = new SqlDataAdapter();
            MydataAdapter.SelectCommand = MyCmd;
            DataSet MyDs = new DataSet();
            MydataAdapter.Fill(MyDs,"db_user");
            for (i = 0; i < MyDs.Tables["db_user"].Rows.Count; i++)
            {
                strRow1 = MyDs.Tables["db_user"].Rows[i]["user"].ToString();
                label1.Text += strRow1;
                strRow2 = MyDs.Tables["db_user"].Rows[i]["pwd"].ToString();
                label1.Text += strRow2;
                if (str1 == strRow1 && str2 == strRow2)
                { flag = 1; break; }
            }
            if (flag == 1)
            {
                this.Hide();
                FrmMain f = (FrmMain)this.ParentForm;
                f.updataMenu();
            }
            else
            {
                MessageBox.Show("帐号/密码错误","错误提示");
                txtuser.Focus();
            }
        }
明明我输入正确,但就是不肯让我登录,气死了
实在是看不出代码错在哪里
搜索更多相关主题的帖子: 帐号 密码 Text String 
2007-12-11 13:22
一刀客
Rank: 1
等 级:新手上路
帖 子:181
专家分:0
注 册:2007-10-26
得分:0 
第一张图有点模糊,点击就会看得清楚

责人之心责己 宽己之心宽人
2007-12-11 13:26
梦心
Rank: 4
来 自:福建平和
等 级:贵宾
威 望:13
帖 子:1910
专家分:0
注 册:2007-5-11
得分:0 
估计是你这个从数据库里面取出来的用户名和密码不对应导致的!!!
我做了个毕业设计,前部分登陆的模式和你一样
你自己看看吧~:
  int i = 0;
        int xuanding;            
        string[] password = new string[3];
private void login_Load(object sender, EventArgs e)
        {
            tianchong();            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == password [xuanding])
            {               
                mainfrm frm = new mainfrm();
                frm.Show();
                this.Hide();
            }
            else
            {
                label3.Visible = true;
                MessageBox.Show("对不起,账户 " + comboBox1.SelectedItem.ToString().Trim() + " 登陆不上,密码错误!","警告",MessageBoxButtons .OK ,MessageBoxIcon.Error);
                textBox1.Clear();               
            }
        }

        private void login_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void tianchong()
        {            
            SqlConnection thisconnection = new SqlConnection("Data Source=(local);Initial Catalog=library;User ID=sa;Password=; ");
            SqlDataAdapter thisadapter = new SqlDataAdapter("select * from info", thisconnection);
            DataSet dt = new DataSet();
            thisadapter.Fill(dt, "users");                             
            foreach (DataRow therow in dt.Tables["users"].Rows)
            {
                password[i] = therow["pwd"].ToString().Trim();
                comboBox1.Items.Add(therow["userid"]);
                i = i+1;
            }
            comboBox1.SelectedIndex = 0;
            xuanding = 0;
            thisconnection.Close();            
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            xuanding = comboBox1.SelectedIndex;
        }

也就是在表里面存有三个账户,都是用户名和密码~
用一个数值password[3]分别将它们一一对应起来,comboBox1_SelectedIndexChanged就是实现了用户名和密码的对应~
你参考下哦

[[italic] 本帖最后由 梦心 于 2007-12-11 14:12 编辑 [/italic]]

我清高和我骄傲的倔强,在风中大声的唱:我不听,我不听~~做我自己最特别,呼呼~~啦啦~~~
我的博客园地址: [url]http://[/url]
2007-12-11 14:09
andey
Rank: 2
等 级:新手上路
威 望:4
帖 子:938
专家分:0
注 册:2007-7-18
得分:0 
break
改为return或者continue试试

msdn == 葵花宝典!!!
QQ:122768959
2007-12-11 14:11
一刀客
Rank: 1
等 级:新手上路
帖 子:181
专家分:0
注 册:2007-10-26
得分:0 
谢谢了~问题解决

责人之心责己 宽己之心宽人
2007-12-11 18:09



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




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

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