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();
答案 0 :(得分:0)
您的INSERT
语句执行了两次:
command.ExecuteNonQuery();
OleDbDataReader reader = command.ExecuteReader();
您应根据要执行的操作删除其中一个呼叫。
您的代码容易受到SQL注入的攻击:
command.CommandText = "insert into Tunnukset (Käyttäjänimi,Salasana) values('" + txtkäyttäjä.Text + "','" + txtsal.Text + "')";
您应该考虑使用参数化查询来摆脱此漏洞。
在执行查询后 后,检查用户名和密码是否为空。
考虑将这些块移动到查询部分之前:
if (txtkäyttäjä.Text == "")
和
if (txtsal.Text == "")
如果要检查名称是否已被使用,应该进行 SELECT
查询。
else if (count > 1)
{
MessageBox.Show("Käyttäjänimi varattu");//username taken
this.Hide();
Form6 frm6 = new 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)
}