2016-10-28 65 views
0

我有一個訪問數據庫,我正在操作與C#。刪除所有訪問數據庫表數據

我已經連接到它,從中檢索了一個數據集,並可以將行添加到表中。現在我正試圖清理一張桌子,但無法使其工作。

我已經試過TRUNCATE TABLE table_name而是拋出異常說我必須使用DELETE, INSERT, PROCEDURE, SELECT or UPDATE,我已經試過Delete FROM table_name然而拋出一個DBConcurrenceyException

這是我必須設法清除表:

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"]); 
} 

我的其他附加方法

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. 
} 
+1

你的命令刪除表你還試圖同時刪除行?你需要做一個或另一個 – Liam

+0

爲什麼使用'OleDbDataAdapter'時,你可以使用'OleDbCommand'發出命令? – stuartd

+4

您並不需要一個適配器,只需執行command.ExecuteNonQuery –

回答

3

你只需要調用ExecuteNonQuery

private void ClearBut_Click(object sender, EventArgs e) 
{ 
    string comand = "DELETE FROM TestTable" 
    OleDbCommand cmd = new OleDbCommand(comand, con); 
    cmd.ExecuteNonQuery(); 

} 
+0

tryed that line cmd.ExecuteNonQuery()給我一個錯誤 在System.Data.dll中發生未處理的System.Data.OleDb.OleDbException類型的異常 其他信息:無效的SQL語句;預期'DELETE','INSERT','PROCEDURE','SELECT'或'UPDATE'。 – skyzzle

+0

@SkylineGodzilla使用sqlcommand這是我的答案 – Mostafiz

+0

的最新更新是你的連接字符串是否正確? –