验证无效后停止插入



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

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();
        }
    }

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

其中一件事与其他事情不同:

        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 组成。

如果电话号码无效,则不会返回,而是继续使用您的方法。 在不重组您正在做的事情的情况下,我可能会这样做,以便所有字段都经过验证,而不是在第一次失败时失败:

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();
    }
}

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

初始化一个变量hasAnyError,在开头的值为false..并在那些"if"块中将其设置为true..并将该SQL块放在最后,如下所示:

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

您不会停止程序流。您需要在显示消息框后立即停止该方法。为此,只需在显示错误的每个消息框之后添加以下内容:

return;

例如:

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

最新更新