我做一个用户登陆界面。按下登陆按钮后执行的代码如下: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(“密码或用户名不正确”);这句话该放哪里;怎么放。多谢
{
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(“密码或用户名不正确”);这句话该放哪里;怎么放。多谢
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(“密码或用户名不正确”);//此处提示错误
}
};
}
{
Form2 f = new Form2();
f.Show();
}
else
{
Messagebox.Show("密码或用户名不正确");
}
{
Form2 f = new Form2();
f.Show();
}
else
{
MessageBox.Show("密码或用户名不正确", "错误提示", MessageBoxButtons.OK);
}
select 语句要加条件的,where username='' and password=''而且你需要用dr.read
用int count=(int)cmd.ExecuteNonQuery();
然后判断count=0的时候,弹提示
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();
{
Form2 f = new Form2();
f.Show();
break;
}
else
{
MessageBox.Show("密码或用户名不正确", "错误提示", MessageBoxButtons.OK);
}
那就改成下面这样: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);
}
}
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("密码或用户名不正确");
如果查出结果了就显示成功,如果受影响行数为0就显示失败(提示密码错误了 )