我的if else声明似乎不起作用.如何防止将空数据发送到数据库?

use*_*396 0 c# database if-statement visual-studio-2010 winforms

 private void btnSubmitConsultation_Click(object sender, EventArgs e)
    {
        int medicalHistoryResult = insertMedicalHistory();

        if (medicalHistoryResult > 0)
        {
            MessageBox.Show("Document(s) submitted", "Success");
        }

        else
        {
            MessageBox.Show("Insert Fail");
        }


        int allergiesResult = insertAllergies();

        if (allergiesResult > 0)
        {
            if (txtNewAllergy.Text != null || txtReactions.Text != null)
            {
                if (txtNewAllergy.Text == null)
                {
                    MessageBox.Show("Please key in the Type of the allergy", "WARNING");
                }
                else if (txtReactions.Text == null)
                {
                    MessageBox.Show("Please key in the Description of the allergy", "WARNING");
                }
            }
            else
            {
                MessageBox.Show("Submitted, fool");
            }
        }
        else
        {
            MessageBox.Show("Not submitted, fool");
        }


    }
Run Code Online (Sandbox Code Playgroud)

那么医疗历史上的结果似乎工作正常,但过敏症结果根本没有做任何事情.

我的insertAlergies函数只是一个普通的INSERT,没什么特别的.

这是我的insertAlergies函数:

private int insertAllergies()
    {
        int allergiesResult = 0;

        string strConnectionString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
        SqlConnection connection = new SqlConnection(strConnectionString);

        try
        {
            string strPatient = "SELECT patientID FROM PATIENT WHERE patientID=@searchPatientID";
            SqlCommand cmdPatient = new SqlCommand(strPatient, connection);
            cmdPatient.Parameters.AddWithValue("@searchPatientID", txtPatientID.Text);

            string strAllergies = "INSERT ALLERGIES (allergyType, allergyDesc, patientID) " +
                "VALUES (@insertType, @insertDesc, @insertPatient)";
            SqlCommand cmdAllergies = new SqlCommand(strAllergies, connection);

            connection.Open();

            cmdAllergies.Parameters.AddWithValue("@insertType", txtNewAllergy.Text);
            cmdAllergies.Parameters.AddWithValue("@insertDesc", txtReactions.Text);

            SqlDataReader readPatient = cmdPatient.ExecuteReader();
            if (readPatient.Read())
            {
                string addPatient = readPatient["patientID"].ToString();
                cmdAllergies.Parameters.AddWithValue("@insertPatient", addPatient);
            }
            readPatient.Close();

            allergiesResult = cmdAllergies.ExecuteNonQuery();


        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }
        finally
        {
            connection.Close();
        }

        return allergiesResult;
    }
Run Code Online (Sandbox Code Playgroud)

-------------------------------------- UPDATE ----------- -------------------------------------

好吧,这是我的新逻辑:

       if (string.IsNullOrEmpty(txtNewAllergy.Text) || string.IsNullOrEmpty(txtReactions.Text))
       {
           if (string.IsNullOrEmpty(txtNewAllergy.Text) && txtReactions.Text != null)
           {
               MessageBox.Show("Please key in the Type of the allergy", "WARNING");
           }
           else if (string.IsNullOrEmpty(txtReactions.Text) && txtNewAllergy.Text != null)
           {
               MessageBox.Show("Please key in the Description  of the allergy", "WARNING");
           }
       }
       else if (txtNewAllergy.Text != null && txtReactions.Text != null)
       {
           int allergiesResult = insertAllergies();
       }
Run Code Online (Sandbox Code Playgroud)

它似乎有效但只有一个缺陷:当我将两个文本都提交为空时,会弹出"请输入过敏类型".如果两个文本都是空的,它怎么做,它什么都不做.

Mar*_*ell 7

如果txtNewAllergytxtReactionsTextBox的话,那就永远不会成为预期的.Textnull,你需要检查一个空的非空字符串.试试string.IsNullOrEmpty(...):

if (!string.IsNullOrEmpty(txtNewAllergy.Text)
    || !string.IsNullOrEmpty(txtReactions.Text))
Run Code Online (Sandbox Code Playgroud)

为方便起见,我们倾向于使用扩展方法:

public static bool HasValue(this string value) {
    return !string.IsNullOrEmpty(value);
}
Run Code Online (Sandbox Code Playgroud)

然后它是:

if (txtNewAllergy.Text.HasValue() || txtReactions.Text.HasValue())
Run Code Online (Sandbox Code Playgroud)

另外:请注意,有一个代码路径什么也不做(请参阅"这里发生了什么?"):

if (allergiesResult > 0)
{
    if (txtNewAllergy.Text != null || txtReactions.Text != null)
    {
        if (txtNewAllergy.Text == null)
        {
            MessageBox.Show("Please key in the Type of the allergy", "WARNING");
        }
        else if (txtReactions.Text == null)
        {
            MessageBox.Show("Please key in the Description of the allergy", "WARNING");
        }
        else
        {
            // WHAT HAPPENS HERE?
        }
    }
    else
    {
        MessageBox.Show("Submitted, fool");
    }
}
else
{
    MessageBox.Show("Not submitted, fool");
}
Run Code Online (Sandbox Code Playgroud)