首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#作的用户登陆界面

2012-12-19 
c#做的用户登陆界面我做一个用户登陆界面。按下登陆按钮后执行的代码如下:if (textBox1.Text || text

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循环才合适
[其他解释]

 


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();  
  }
  else
  {
    Messagebox.Show(“密码或用户名不正确”);//此处提示错误
  }
  };   
  }


[其他解释]
读出数据后,和用户输入的密码,用户名相比对。

不对,弹出提示。
[其他解释]
不行的呀。我试过。就算正确他也会弹出messagebox,那个while循环还没结束。
[其他解释]
把循环去掉
[其他解释]
在这个地方:

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

[其他解释]
if (ss[0] == textBox1.Text && ss[1] == textBox2.Text)
  {
  Form2 f = new Form2();
  f.Show();  
  }
else
{
MessageBox.Show("密码或用户名不正确", "错误提示", MessageBoxButtons.OK); 
}

[其他解释]
                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();
[其他解释]
不需要用循环,不需要用dr.read

[其他解释]
select count(*) from password where username='?' and password='?'
[其他解释]
select count(*) from password where username='?' and password='?'
[其他解释]
那是因为你没有跳出循环,当你第一次对了后,马上就跳出循环就可以了。
[其他解释]
if (ss[0] == textBox1.Text && ss[1] == textBox2.Text)
  {
  Form2 f = new Form2();
  f.Show();  
 break;
  }
else
{
MessageBox.Show("密码或用户名不正确", "错误提示", MessageBoxButtons.OK);  
}
[其他解释]
来晚了一步,大家都说了,顶一下
[其他解释]
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("密码或用户名不正确");
[其他解释]

引用:
不行的呀。我试过。就算正确他也会弹出messagebox,那个while循环还没结束。


那就改成下面这样:

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);
}
  
}

[其他解释]
你取出DB中正确的密码之后
就可以用其与输入的密码比较 
不同就可以提示密码错误了  
[其他解释]
错了,是int find = 0

ly320老兄,你的if else是不是会让楼主拼命的弹出提示来阿
[其他解释]
以前做过  就是把这些信息存入数据库  查询数据库  把用户名和密码作为查询条件
查询结果为0则提示错误,否则弹出登录后的系统界面,还有权限什么的用一个字段来记录  设置不同的权限 读取时 根据权限给他可以操作的范围
[其他解释]
个人认为应该把输入的用户名和密码做为Sql语句的条件:string sql = "select * from Users where LoginId=@LoginId and LoginPwd=@LoginPwd";
如果查出结果了就显示成功,如果受影响行数为0就显示失败(提示密码错误了 )

热点排行