简体   繁体   中英

C# deleting rows in DataTable

I delete rows in a DataTable with this code:

if (x == "y" || x == "yes")           
{             
    dt.Rows.RemoveAt(id);  
}

How I can save changes (delete row) in MS Access? I have code like this, but this doest not work

OleDbCommand delCmd = new OleDbCommand("DELETE FROM PERSONA WHERE ID=?",con1);
delCmd.Parameters.Add(new OleDbParameter("@ID",OleDbType.Integer, @ID));

foreach (DataRow dro in dt.GetChanges().Rows)           
{
   if (dro.RowState == DataRowState.Deleted)                                                   
   {    
      con1.Open();    
      delCmd.ExecuteNonQuery();    
      con1.Close();
   }    
}

You're not specifying the correct ID. You need to add the parameter to the command within your loop.

Delete the rows using the Delete method. This will mark the rows to be deleted when using Update on a table adapter. You are just removing rows from the collection; not from the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM