添加到Access时检查现有值

时间:2016-03-05 18:03:00

标签: c#

我有一个界面,其中一个功能包括从Access中删除序列号。如果序列号确实存在,那么它将通过确认消息删除所有内容。问题是,我可以键入一个不存在的序列号,它的行为就像删除它一样。单击删除按钮时如何检查值是否存在,以便我可以向用户发送通知?

private void btnDelete_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtSerial.Text))

            try
            {
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string deleteEntry = "delete from Inventory where SerialNumber='" + txtSerial.Text + "' ";
                DialogResult result = MessageBox.Show("ARE YOU SURE YOU WANT TO DELETE SERIAL NUMBER = " + txtSerial.Text + " ? ", "LAST CHANCE !", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

                if (result.Equals(DialogResult.OK))
                {
                    command.CommandText = deleteEntry;
                    command.ExecuteNonQuery();
                    MessageBox.Show("Data Has Been Deleted".PadLeft(28));
                }

                if (dataGridFB.DataSource != null)
                {
                    dataGridFB.DataSource = null;
                    txtSerial.Clear();
                    comboSerial.Text = string.Empty;
                    comboPart.Text = string.Empty;
                    comboRO.Text = string.Empty;
                    comboLocation.Text = string.Empty;
                    comboSerial.Items.Clear();
                    comboPart.Items.Clear();
                    comboRO.Items.Clear();
                    comboLocation.Items.Clear();
                }
                else
                {
                    dataGridFB.Rows.Clear();

                }

                ItemsLoad();
                connection.Close(); // CLOSE HERE OR YOU CANNOT ENTER RECORDS SIMULTANEOUSLY

            }
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Message);
                connection.Close();
            }
    }

3 个答案:

答案 0 :(得分:3)

OleDbCommand.ExecuteNonQuery返回受命令影响的行数。如果没有要删除的行,则返回值将为零。所以很容易发现这种情况

int rowsDeleted = command.ExecuteNonQuery();
if(rowsDeleted == 0)
    MessageBox.Show("No Serial number found to delete");
else
    ....

说,记住用于构建命令文本的字符串连接被认为是不好的做法,你永远不应该使用它。参数化查询是创建需要用户输入的命令的唯一正确方法.....

string deleteEntry = "delete from Inventory where SerialNumber=@num"
command.CommandText = deleteEntry;
command.Parameters.Add("@num", OleDbType.VarWChar).Value =  txtSerial.Text;
int deletedRows = command.ExecuteNonQuery();

答案 1 :(得分:0)

您正在使用的OleDbCommand.ExecuteNonQuery方法返回Int32,表示受影响的行数。您可以使用它来确定是否删除了一行。

if (command.ExecuteNonQuery() > 0) 
{
    // Row was deleted
}
else
{
    // Row was not deleted
}

答案 2 :(得分:0)

您可以在基于if的语句中执行Console.WriteLine("Nothing to delete"),但如果它已编译但您看不到控制台,请在基于if语句后尝试MessageBox.Show("Nothing to delete")函数。

        if (command.ExecuteNonQuery == 0)
    {
        Console.WriteLine("0-removed")
    }
else
    {
        Console.WriteLine("not deleted")

作为一个例子......

    private void timer1_Tick(object sender, EventArgs e)
        if (command.ExecuteNonQuery == 0)
    {
        Console.WriteLine("0-removed")
    }
else
    {
        Console.WriteLine("not deleted")