0

I have an access database that I am manipulating with C#.

I have connected to it, retrieved a data-set from it and can add rows to a table. Now I am trying to clear a table and I am unable to get it to work.

I have tried TRUNCATE TABLE table_name but that throws an exception saying that I must use either DELETE, INSERT, PROCEDURE, SELECT or UPDATE and I have tried Delete FROM table_name However that throws an DBConcurrenceyException.

Here is what I have to tried to clear the table:

private void ClearBut_Click(object sender, EventArgs e)
{
    OleDbDataAdapter dtaAdpTestTableClear = new OleDbDataAdapter();
    OleDbCommand command;

    command = new OleDbCommand("DELETE FROM TestTable", con);

    dtaAdpTestTableClear.DeleteCommand = command;

    foreach (DataRow row in dsWCSDHDB.Tables["TestTable"].Rows)
    {
        row.Delete();
    }

    dtaAdpTestTableClear.Update(dsWCSDHDB.Tables["TestTable"]);
}

My other add method

private void Add_Click(object sender, EventArgs e)
{
    OleDbDataAdapter dtaAdpTestTableInsertNewRow = new OleDbDataAdapter();
    OleDbCommand command;

    // Create the InsertCommand.
    // This is needed as DataAdaptor.InsertCommand() is called during the update to insert the row into the database. It requires an insert query
    command = new OleDbCommand("INSERT INTO TestTable (id, someData) " +"VALUES (?, ?)", con); //We create a dbcommand the command is, Querytype, what we are doing with it, what table, (columns we are using), concat, Values we will be adding(as ? for now as we will pass this data in latter), connection to the database 

    command.Parameters.Add("id", OleDbType.Char, 5, "id"); //this is where we add a parameter to the command function. we add one per column in the row (columns we are using name, value type, column length, source column, these parameters will replace the ? in the query above 
    command.Parameters.Add("someData", OleDbType.VarChar, 40, "someData");

    dtaAdpTestTableInsertNewRow.InsertCommand = command;// we attach this command to the Insert command function of the adapter that we are using

    //Create the new row
    DataRow  row = dsWCSDHDB.Tables["TestTable"].NewRow(); //Create a new empty row that is formated for the TestTable table
    row["someData"] = AddValueTextBox.Text.ToString();// add in the values 

    //Add the new row to the dataset table
    dsWCSDHDB.Tables["TestTable"].Rows.Add(row); //adds this new row to the clients dataset

    //Updates the database table with the values of the clients dataset Table
    //For this to work you need to build a proper data adapter that is using a query taylered for the table you are using. 
    //Unfortunately although it would be nice to be able to add and use tables to the database with out changing the code you cant build a generic one that works for all tables in the database. 
    //this is because different tables can have different fields and column lengths .
    //there is a example of how to build one below 

    //Update the database table with the values of the clients dataset Table
    dtaAdpTestTableInsertNewRow.Update(dsWCSDHDB.Tables["TestTable"]);   // using the adapter that we created above we update the database with the clients dataset.
}
5
  • 1
    Your command deletes a table yet your also trying to delete the rows at the same time?! You need to do one or the other Commented Oct 28, 2016 at 10:33
  • Why use a OleDbDataAdapter when you can just issue the command using the OleDbCommand? Commented Oct 28, 2016 at 10:33
  • 4
    You don't really need an adapter, just execute command.ExecuteNonQuery Commented Oct 28, 2016 at 10:33
  • Im not trying to delete the table I am trying to deleat all the rows in the table. I dont know how to do it with out a data adaptor Commented Oct 28, 2016 at 10:36
  • command.ExecuteNonQuery(); Commented Oct 28, 2016 at 10:37

1 Answer 1

3

You will just need to call ExecuteNonQuery

private void ClearBut_Click(object sender, EventArgs e)
{
    string comand = "DELETE FROM TestTable";
    OleDbCommand cmd = new OleDbCommand(comand, con);
    cmd.ExecuteNonQuery();
 }
Sign up to request clarification or add additional context in comments.

10 Comments

tryed that however the line cmd.ExecuteNonQuery() gives me an error An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll Additional information: Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.
@SkylineGodzilla use sqlcommand which is in latest update of my answer
is your connection string proper?
@Mostafiz well thats a first I got a purple underline. my app is setup to use an OleDB conection not a sql conection.as its conecting to an access database not an sql database can not convert oledbconection to sqlconection
@SkylineGodzilla but OleDbCommand should work which I have come back again, ok I am checking again
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.