Barak Yaari
Barak Yaari

Reputation: 418

Is there a way to delete all the rows in a table using c# and MS Access?

I want to write a simple code line that will delete all the datarows in a certain Table. (Working with c# and MS Access 2010) The table contains about 1000 rows, 10 columns, and it gets a bit slow.

This code does the job:

  connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Words\shinunonDB.accdb;Persist Security Info=False;";

  OleDbConnection Conn = new OleDbConnection();
  Conn.ConnectionString = connStr;
  sql = "select * from Heb";
  Conn.Open();
  OleDbDataAdapter da = new OleDbDataAdapter(sql, Conn);
  DataSet ds = new DataSet();
  OleDbCommandBuilder cb = new OleDbCommandBuilder(da);

  da.Fill(ds, "Heb");
  foreach (DataRow dRow in ds.Tables["heb"].Rows)
  {
      dRow.Delete();
  }
  da.Update(ds, "heb");

But I guess there might be a shorter and more efficient way to do it.

Thank you guys.

Upvotes: 1

Views: 5869

Answers (2)

m3xpl4y
m3xpl4y

Reputation: 68

For me it works using the following lines:

OleDbCommand ac = new OleDbCommand("delete from Heb",Conn);
ac.ExecuteNonQuery();

But it deletes all my data from the table, not just a row.

and when I do this

conn.Open();
OleDbCommand cmd = new OleDbCommand("delete from Kunden where id=" + txtKdnID.Text + " ", conn);
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("Eintrag erfolgreich gelöscht");

I get this error.

Upvotes: 0

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181460

Yes, there is. Just use a SQL command:

delete from Heb;

You can issue a SQL command on a OleDbConnection. Something like this:

OleDbCommand ac = new OleDbCommand("delete from Heb",Conn);
ac.ExecuteNonQuery();

Upvotes: 10

Related Questions