
command.CommandText = "insert into Tunnukset (Käyttäjänimi,Salasana) values('" + txtkäyttäjä.Text + "','" + txtsal.Text + "')";
command.ExecuteNonQuery(); //command.CommandText = "insert into Tunnukset (Käyttäjänimi,Salasana) values('" + txtkäyttäjä.Text + "','" + txtsal.Text + "')";
OleDbDataReader reader = command.ExecuteReader();
int count = 1;
while (reader.Read())
{
count++;
}
if (txtkäyttäjä.Text == "")
{
MessageBox.Show("Käyttäjänimi kentt� tyhj�");//username field empty
this.Hide();
Form6 frm6 = new Form6();
frm6.ShowDialog();
}
if (txtsal.Text == "")
{
MessageBox.Show("Salasana kentt� tyhj�");//passoword field empty
this.Hide();
Form6 frm6 = new Form6();
frm6.ShowDialog();
}
else if (count > 1)
{
MessageBox.Show("Käyttäjänimi varattu");//username taken
this.Hide();
Form6 frm6 = new Form6();
frm6.ShowDialog();
}
if (txtsal.Text != txtvarmista.Text)
{
MessageBox.Show("salasana ei t�sm��");//password do not match
}
else if (count == 1)
{
MessageBox.Show("Tunnusten luominen onnistui");//Signup successfull
con.Close();
this.Hide();
Form5 f5 = new Form5();
f5.ShowDialog();发布于 2019-04-29 17:35:49
根据@Steve的说明进行回答
您的INSERT语句将执行两次:
command.ExecuteNonQuery();
OleDbDataReader reader = command.ExecuteReader();根据之后要做的事情,您应该删除其中一个调用。
你应该考虑的其他东西
command.CommandText =“插入到Tunnukset (Käyttäjänimi,Salasana)值( '”+ txtkäyttäjä.Text + "',‘“+ txtsal.Text + "')";
您应该考虑使用参数化查询来消除这种用户名和密码为空的vulnerability.
考虑在查询部分之前移动这些块:
if (txtkäyttäjä.Text == "")
和
if (txtsal.Text ==“”)
SELECT查询。else if (计数> 1) { MessageBox.Show("Käyttäjänimi varattu“);//用户名为this.Hide();Form6 frm6 =新的Form6();frm6.ShowDialog();}
您正在告诉用户,在插入用户名之后,他的用户名已经被了,所以已经太迟了。
伪代码尝试
if(string.IsNullOrEmpty(username))
{
// tell username is empty
}
else if(string.IsNullOrEmpty(password))
{
// tell password is empty
}
else if(password != passwordConfirmation)
{
// tell passwords do not match
}
else if(UserAlreadyExists(username))
{
// tell username is taken
}
else
{
if(InsertUser(username, password))
{
// tell sign-in successfull
}
}
private bool UserAlreadyExists(string username)
{
// use SELECT parameterized query
// return user existance (true or false)
}
private bool InsertUser(string username, string password)
{
// use INSERT parameterized query
// return success (true or false)
}https://stackoverflow.com/questions/55900032
复制相似问题