2012-08-12 52 views
0

我想在无效验证后停止插入数据库。就像下面的代码片段显示无效电话号码的消息,但它仍将代码插入到数据库中。还有一个问题,我想在我的程序中应用这个规则,我认为这个程序存在这个潜在的错误。无效验证后停止插入

private void button1_Click(object sender, EventArgs e) 
     { 
      Regex regexObj = new Regex(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$"); 

      if (regexObj.IsMatch(textBox3.Text)) 
      { 
       string formattedPhoneNumber = 
        regexObj.Replace(textBox3.Text, "($1) $2-$3"); 
      } 
      else 
      { 
       MessageBox.Show("Invalid Phone Number! \nFormat is (XXX) XXX-XXXX"); 
      } 

      if (string.IsNullOrEmpty(textBox1.Text) || textBox1.Text.Trim().Length == 0) 
      { 
       MessageBox.Show("Field can't be left blank!"); 
       return; 
      } 
      if (string.IsNullOrEmpty(textBox2.Text) || textBox2.Text.Trim().Length == 0) 
      { 
       MessageBox.Show("No Name for the Author!"); 
       return; 
      } 

      SqlConnection con = new SqlConnection("Server = DAFFODILS-PC\\SQLEXPRESS;Database=Library;Trusted_Connection=True;"); 
      SqlCommand sql1 = new SqlCommand("INSERT into Members VALUES('" + textBox1.Text + "' , '" + textBox2.Text + "','" + textBox3.Text + "')", con); 
      con.Open(); 
      sql1.ExecuteNonQuery(); 
      con.Close(); 
      this.membersTableAdapter.Fill(this.booksDataSet.Members); 
      MessageBox.Show("Data Added!"); 
      textBox1.Text = ""; 
      textBox2.Text = ""; 
      textBox3.Text = ""; 
      textBox1.Focus(); 

     } 
    } 

我想,我必须使用这种方法,但我不知道如何。有什么建议么?

+0

您的程序有SQL注入问题。谷歌“SQL注入”,然后用参数化查询修复问题,否则你将陷入一个受到伤害的世界。 – Hogan 2012-08-12 06:38:36

+0

我并不担心这一点。 :-) – unknownsatan 2012-08-12 06:46:11

回答

3

这些事情之一是不喜欢别人:

 else 
     { 
      MessageBox.Show("Invalid Phone Number! \nFormat is (XXX) XXX-XXXX"); 
     } 

     if (string.IsNullOrEmpty(textBox1.Text) || textBox1.Text.Trim().Length == 0) 
     { 
      MessageBox.Show("Field can't be left blank!"); 
      return; 
     } 
     if (string.IsNullOrEmpty(textBox2.Text) || textBox2.Text.Trim().Length == 0) 
     { 
      MessageBox.Show("No Name for the Author!"); 
      return; 
     } 

注意一下两个验证中有MessageBox.Show之后立即return?那些是从方法返回而不插入记录的那些。请注意电话号码验证如何没有return?这就是为什么它显示消息然后插入 - 因为这是你告诉它做的。你没有告诉它在显示信息后停止;你只要让它继续运行其余的方法,主要由INSERT组成。

+0

啊!我没有注意到,非常感谢! :d – unknownsatan 2012-08-12 03:06:17

0

您只是检查错误并显示错误消息。

Intialize与在开始时的错误值的变量hasAnyError ..它设置为true,这些“如果”块..并把该SQL块到底象下面这样:

if (!hasAnyError) { 
    //put all that sql block here 
} 
0

你没有停止程序流程。您需要在显示消息框后立即停止该方法。要做到这一点,仅仅是显示错误每次的MessageBox后补充一点:

return; 

e.g:

MessageBox.Show("Invalid Phone Number! \nFormat is (XXX) XXX-XXXX"); 
return; 
1

你不返回如果电话号码是无效的,但继续在你的方法。如果没有重构你所做的太多工作,我可能会这样做,因此所有字段都会在验证失败时通过验证而不是失败:

private void button1_Click(object sender, EventArgs e) 
    { 
     List<string> validationErrors = new List<string>(); 

     Regex regexObj = new Regex(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$"); 

     if (regexObj.IsMatch(textBox3.Text)) 
     { 
      string formattedPhoneNumber = 
       regexObj.Replace(textBox3.Text, "($1) $2-$3"); 
     } 
     else 
     { 
      validationErrors.Add("Invalid Phone Number! \nFormat is (XXX) XXX-XXXX"); 
     } 

     if (string.IsNullOrEmpty(textBox1.Text) || textBox1.Text.Trim().Length == 0) 
     { 
      validationErrors.Add("Field can't be left blank!"); 
     } 
     if (string.IsNullOrEmpty(textBox2.Text) || textBox2.Text.Trim().Length == 0) 
     { 
      validationErrors.Add("No Name for the Author!"); 
     } 

     if (validationErrors.Count > 0) 
     { 
      MessageBox.Show(string.Join(Environment.NewLine, validationErrors.ToArray())); 
      return; 
     } 

     SqlConnection con = new SqlConnection("Server = DAFFODILS-PC\\SQLEXPRESS;Database=Library;Trusted_Connection=True;"); 
     SqlCommand sql1 = new SqlCommand("INSERT into Members VALUES('" + textBox1.Text + "' , '" + textBox2.Text + "','" + textBox3.Text + "')", con); 
     con.Open(); 
     sql1.ExecuteNonQuery(); 
     con.Close(); 
     this.membersTableAdapter.Fill(this.booksDataSet.Members); 
     MessageBox.Show("Data Added!"); 
     textBox1.Text = ""; 
     textBox2.Text = ""; 
     textBox3.Text = ""; 
     textBox1.Focus(); 

    } 
}