2012-08-12 63 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(); 

    } 
}