日期:2014-05-17  浏览次数:20929 次

c#做的用户登陆界面
我做一个用户登陆界面。按下登陆按钮后执行的代码如下:

if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show("请输入用户名和密码");
            }
            else
            {
              
                string s ="Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\c#程序 \\zhuce\\zhuce\\username.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                SqlCommand da;
                SqlDataReader dr;
                SqlConnection conn = new SqlConnection(s);
                da = new SqlCommand("select * from password", conn);
                conn.Open();
                dr = da.ExecuteReader();
                while (dr.Read())
                {
                    string[] ss = new string[] { dr.GetString(0).Trim(), dr.GetString(1).Trim() };
                    if (ss[0] == textBox1.Text && ss[1] == textBox2.Text)
                    {
                        Form2 f = new Form2();
                        f.Show(); 
                    }
                };       
            }

我想在密码错误时执行:messagebox。show(“密码或用户名不正确”);这句话该放哪里;怎么放。多谢
------最佳解决方案--------------------
da = new SqlCommand("select * from password ", conn);
select 语句要加条件的,where username='' and password=''

而且你需要用dr.read
用int count=(int)cmd.ExecuteNonQuery();
然后判断count=0的时候,弹提示
------其他解决方案--------------------
大家看清楚,这有个while循环啊。就算第一次对了。他还要读数据库里的数据来比较的。可是我不知道在那里break掉while循环才合适
------其他解决方案--------------------