Search code examples
c#ms-accessoledboledbconnectionoledbcommand

Couldn't delete all rows of a table from Microsoft Access via OleDB in C#


I had a method in which I intend for it to delete all data from the table. However, even as it is called, the deletion didn't happen at all.

Here below is the method. Let's assume that the data is already loaded in the table. Table "CartListClone" has 5 columns (excluding the ID). The table as you can see in the connection string derives from Access.

public void deleteEverything() {
        OleDbConnection connect =
            new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; 
                Data Source=POSDB.accdb; 
                Persist Security Info = False");
        connect.Open();

        OleDbCommand command = new OleDbCommand();
        command.Connection = connect;
        command.CommandText = "DELETE * FROM CartListClone";

    }

As of now, I feel that the problem is rooted at the method. Is there something that I did wrong here? Much appreciated for any help.

UPDATE: Following sstan's suggestion, here below is the rewritten method.

public void deleteEverything() {
        OleDbConnection connect =
            new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; 
                Data Source=POSDB.accdb; 
                Persist Security Info = False");
        connect.Open();

        OleDbCommand command = new OleDbCommand("DELETE * FROM CartListClone");
        command.Connection = connect;
        command.ExecuteNonQuery();
    }

Solution

  • You're never executing the command object. You're missing this line at the end:

    command.ExecuteNonQuery();