【发布时间】:2013-11-25 21:47:51
【问题描述】:
如果 (count == 1) 让它做与目前相同的事情,但不是隐藏,而是关闭和打开另一个表单 (e.x form2),我该如何做?
我已经试过了。关闭();但它最终在打开新表单的代码运行之前关闭了程序。
private void button1_Click(object sender, EventArgs e)
{
try
{
string myConnection = "datasource=localhost;port=3306;username=dolfin;password=quack";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand SelectCommand = new MySqlCommand("select * from database1.logins where username='" + this.username_txt.Text + "' and password='" + this.password_txt.Text + "' ;", myConn);
MySqlDataReader myReader;
myConn.Open();
myReader = SelectCommand.ExecuteReader();
int count = 0;
while (myReader.Read())
{
count = count + 1;
}
if (count == 1)
{
MessageBox.Show("Username and password is correct.");
this.Hide();
Form2 f2= new Form2();
f2.ShowDialog();
}
else if (count > 1)
{
MessageBox.Show("Duplicate Username and Password... Access Denied!");
}
else
MessageBox.Show("Username and password is not correct... please try again.");
myConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
【问题讨论】:
-
您的代码易受 SQL 注入攻击。
-
关闭主窗体会结束它的消息循环,代码会从
Application.Run(yourMainForm);跳出你的窗体不用关闭,隐藏就够了,不要觉得浪费记忆,我觉得这里不值一提。 -
我需要关闭它,如果消息不显示也没关系。
-
该代码非常容易受到攻击!特地在登录!如果在密码文本框中我写“PASSWORD OR 1 = 1”,则用户名和密码将是正确的。
-
我猜这种代码在一些学校里是作为新手入门的。
标签: c#