如何检查数据库中是否存在数据并显示错误

时间:2019-04-23 15:44:07

标签: c# winforms

我正在尝试显示一个错误,如果用户提交同一日期的数据,则不应允许他们这样做。 我不是很确定如何做到这一点,但给了它一个刺但不起作用。我不确定如何使读者工作。

public string conString = "***ConnectionString***";

    private void mileage_submit_btn_Click(object sender, EventArgs e)
    {
        if (location_comboBox.Text == "" || mileage_textbox.Text == "" || other_location_textBox.Text == "" || other_mileage_textBox.Text == "")
        {
            MessageBox.Show("Fill all required fields", ("Validation"), MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            if (con.State == System.Data.ConnectionState.Open)

            {
                string query = "INSERT INTO Mileage_Table(eb_number , Date, Location, Mileage, Other_location, Other_Miles, Total_Miles) values(@eb_number, @Date, @Location, @Mileage, @Other_location,@Other_Miles, @Total_Miles)";
                SqlCommand cmd = new SqlCommand(query, con);
                cmd.Parameters.Add("@eb_number", SqlDbType.Text).Value = Username_Mileage_lbl.Text.ToString();
                cmd.Parameters.Add("@Location", SqlDbType.Text).Value = location_comboBox.Text.ToString();
                cmd.Parameters.Add("@Mileage", SqlDbType.Decimal).Value = Decimal.Parse(mileage_textbox.Text);
                cmd.Parameters.Add("@Other_Location", SqlDbType.Text).Value = other_location_textBox.Text.ToString();
                cmd.Parameters.Add("@Other_Miles", SqlDbType.Decimal).Value = Decimal.Parse(other_mileage_textBox.Text);
                cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Parse(Mileage_dateTimePicker.Text).ToString("dd-MM-yyyy");
                decimal b = Convert.ToDecimal(mileage_textbox.Text);
                decimal c = Convert.ToDecimal(other_mileage_textBox.Text);
                cmd.Parameters.Add("@Total_Miles", SqlDbType.Decimal).Value = Convert.ToString(b + c);


                using (var reader = cmd.ExecuteReader(System.Data.CommandBehavior.SingleRow))
                {
                    while (reader.Read())
                    {
                        if (reader.HasRows == true)
                        {
                            MessageBox.Show("Date = " + reader[7].ToString() + "Already Exists");
                        }
                    }
                }
                cmd.ExecuteNonQuery();
                decimal TotalMiles = b + c;
                MessageBox.Show(TotalMiles + " Miles has been Logged");
                con.Close();

1 个答案:

答案 0 :(得分:1)

假设您使用的是Sql Server,一种使用以下SQL的方式即可实现

If NOT Exists(select from table where datecolumn = @myDateParam)
    Insert Into table (col list) values (@param list);

然后您使用

调用此sql
int retVal = cmd.ExecuteNonQuery( ..  ); // for plain sql number of affected rows returned. 0 - would mean nothing inserted
MessageBox.Show((retVal > 0 ? "Record Inserted" : "Record with specific date already existed"));

现在,如果要使用cmd.ExecuteReader输出插入的值,则需要在插入的Sql OUTPUT INSERTED子句https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql?view=sql-server-2017中添加