c#做的用户登陆界面

lpt1122 2010-04-22 10:23:56
我做一个用户登陆界面。按下登陆按钮后执行的代码如下:

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(“密码或用户名不正确”);这句话该放哪里;怎么放。多谢
...全文
487 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
guiwang24 2012-12-04
  • 打赏
  • 举报
回复
说实话 你的那个循环做的有点多余 我看楼上有很多建议是去掉循环 这样做更好一些 楼主可以试试
只是空想家 2012-12-04
  • 打赏
  • 举报
回复
以前做过 就是把这些信息存入数据库 查询数据库 把用户名和密码作为查询条件 查询结果为0则提示错误,否则弹出登录后的系统界面,还有权限什么的用一个字段来记录 设置不同的权限 读取时 根据权限给他可以操作的范围
斑斓 2010-04-22
  • 打赏
  • 举报
回复
个人认为应该把输入的用户名和密码做为Sql语句的条件:string sql = "select * from Users where LoginId=@LoginId and LoginPwd=@LoginPwd";
如果查出结果了就显示成功,如果受影响行数为0就显示失败(提示密码错误了 )
zhehan54 2010-04-22
  • 打赏
  • 举报
回复
你取出DB中正确的密码之后
就可以用其与输入的密码比较
不同就可以提示密码错误了
shang123guan 2010-04-22
  • 打赏
  • 举报
回复
错了,是int find = 0

ly320老兄,你的if else是不是会让楼主拼命的弹出提示来阿
shang123guan 2010-04-22
  • 打赏
  • 举报
回复
bool find= 0;
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();
find = 1;
break;
}
}
if(find == 0)
Messagebox.Show("密码或用户名不正确");
皇城龙三 2010-04-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lpt1122 的回复:]
不行的呀。我试过。就算正确他也会弹出messagebox,那个while循环还没结束。
[/Quote]

那就改成下面这样:

if (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();
}
else
{
MessageBox.Show("密码或用户名不正确!", "错误提示", MessageBoxButtons.OK);
}
}
else
{
MessageBox.Show("用户名不存在,请重新输入!", "错误提示", MessageBoxButtons.OK);
}

}
myhope88 2010-04-22
  • 打赏
  • 举报
回复
来晚了一步,大家都说了,顶一下
chuangwan 2010-04-22
  • 打赏
  • 举报
回复
if (ss[0] == textBox1.Text && ss[1] == textBox2.Text)
{
Form2 f = new Form2();
f.Show();
break;
}
else
{
MessageBox.Show("密码或用户名不正确", "错误提示", MessageBoxButtons.OK);
}
chuangwan 2010-04-22
  • 打赏
  • 举报
回复
那是因为你没有跳出循环,当你第一次对了后,马上就跳出循环就可以了。
LoveLife_Go 2010-04-22
  • 打赏
  • 举报
回复
select count(*) from password where username='?' and password='?'
LoveLife_Go 2010-04-22
  • 打赏
  • 举报
回复
select count(*) from password where username='?' and password='?'
qshurufa 2010-04-22
  • 打赏
  • 举报
回复
把循环去掉
LoveLife_Go 2010-04-22
  • 打赏
  • 举报
回复
不需要用循环,不需要用dr.read
zhenonline 2010-04-22
  • 打赏
  • 举报
回复
string uname = this.txtUserName.Text.ToString();
string upassword = this.txtPsw.Text.ToString();
SqlConnection thisconnection = new SqlConnection(@"Server=.;database=brmine;uid=sa;pwd=123456");
thisconnection.Open();
SqlCommand thiscommand = thisconnection.CreateCommand();
thiscommand.CommandText = "select count(*) from UserLogin where UserName='" + txtUserName.Text.Trim() + "' and Passwd='" + txtPsw.Text.Trim() + "'";
SqlDataReader thisreader = thiscommand.ExecuteReader();
if (thisreader.Read())
{
if (thisreader["password"].ToString().Trim() == upassword)
{
MessageBox.Show("恭喜您登陆成功!", "登陆成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
brMainInfoManage tt = new brMainInfoManage();
tt.Show();

}
else { MessageBox.Show("密码错误,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information); }

}
else { MessageBox.Show("此用户不存在,请您注册!", "注册", MessageBoxButtons.OK, MessageBoxIcon.Information); }
thisconnection.Close();
thisreader.Close();
LoveLife_Go 2010-04-22
  • 打赏
  • 举报
回复
da = new SqlCommand("select * from password ", conn);
select 语句要加条件的,where username='' and password=''

而且你需要用dr.read
用int count=(int)cmd.ExecuteNonQuery();
然后判断count=0的时候,弹提示
lpt1122 2010-04-22
  • 打赏
  • 举报
回复
大家看清楚,这有个while循环啊。就算第一次对了。他还要读数据库里的数据来比较的。可是我不知道在那里break掉while循环才合适
皇城龙三 2010-04-22
  • 打赏
  • 举报
回复
if (ss[0] == textBox1.Text && ss[1] == textBox2.Text)
{
Form2 f = new Form2();
f.Show();
}
else
{
MessageBox.Show("密码或用户名不正确", "错误提示", MessageBoxButtons.OK);
}
lpt1122 2010-04-22
  • 打赏
  • 举报
回复
不行的呀。我试过。就算正确他也会弹出messagebox,那个while循环还没结束。
clming327 2010-04-22
  • 打赏
  • 举报
回复
在这个地方:

if (ss[0] == textBox1.Text && ss[1] == textBox2.Text)
{
Form2 f = new Form2();
f.Show();
}
else
{
Messagebox.Show("密码或用户名不正确");
}
加载更多回复(2)

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧