问题
Hello everone
I have a function
public void RemoveStock(string tickerName) {
int key = this.getKeyFromtickerName(tickerName);
foreach (var item in listingDataTable.Where(
x => x.stock == key).ToList()) {
listingDataTable.RemoveListingRow(item);
}
listingTa.Update(listingDataTable);
}
Definitions:
listingDataTable = new stockDataSet.ListingDataTable();
listingTa = new stockDataSetTableAdapters.ListingTableAdapter();
Database type: SQL Server Compact Edition
The function remove rows from the dataset, but not from the database. All additions to the database is stored but I can't delete from the database.
Do I need to do something else to make the database to accept my changes?
Best Regards
Gorgen
回答1:
Try this instead:
foreach (var item in listingDataTable.Where(
x => x.stock == key).ToList()) {
item.Delete();
}
I think this will actually mark the row for deletion.
回答2:
Turned out that I needed to add an DeleteCommand in my database adapter. The DeleteCommand wasn't automaticly generated and when using .RemoveListingRow(item)
no error message was issued, it just didn't work.
In Data Sources choose "Edit DataSet with Designer" Properties of tableAdaper generate command such as
DELETE FROM Listing WHERE (stock = @p1)
来源:https://stackoverflow.com/questions/7834376/deleted-item-in-dataset-not-deleted-in-database