Why won't this DataAdapter insert rows into the database?

前端 未结 2 1956
孤独总比滥情好
孤独总比滥情好 2021-01-21 23:05

So I have a situation where I am using a SqlDataAdapter to insert rows into a table in a SQL Server 2014 database.

The source of the data is an Excel spreadsheet.

<
2条回答
  •  春和景丽
    2021-01-22 00:05

    Each DataTable tracks the RowState of its rows, so manually adding data in a loop works because they are all Added (it has nothing to do with manually creating the DataTable - its the rows). When you load from some other source like Excel, they are not added/new.

    If you use a DataAdapter to fill the table, you can tell it not to set the RowState to Unchanged. This is very useful for migrating data from one data store to another:

    myDA.AcceptChangesDuringFill = False
    ...
    rows = myDA.Fill(xlDT)
    

提交回复
热议问题