deleted item in dataset not deleted in database

两盒软妹~` 提交于 2019-12-25 02:12:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!