Search code examples
c#system.data.datatable

Updating DataTable


for(int i = 0; i < m_DataTable.Rows.Count; i++)
{
    m_DataTable.Rows[i]["WORKER"] = "test";
    m_DataTable.Rows[i].AcceptChanges();
}

m_DataTable.AcceptChanges();

Is there any reason that I can not see that this code does not update my dataTable?


Solution

  • Shouldn't you be creating a data adaptor and call Update to update the dataset changes to your database, before you call AcceptChanges?

    Something like:

    using (DataAdaptor adaptor = new DataAdaptor("SELECT * FROM table", connection)) {
        using (CommandBuilder builder = new CommandBuilder(adaptor)) {
            adaptor.Update(m_DataTable);
        }
    }
    
    m_DataTable.AcceptChanges();