问题描述:

i'm trying to add a new row to my datatable.

I've no error while running this code but nothing happened in my table.

Here's my code :

 string table = "`DONNEE ENTRANT`";

OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * From " + table, _conn);

OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(adapter);

DataTable dataTable = new DataTable(table);

adapter.Fill(dataTable);

DataRow row = dataTable.NewRow();

row["CODE LIAISON"] = "TEST";

dataTable.Rows.Add(row);

dataTable.AcceptChanges();

adapter.Update(dataTable);

Any idea what i'm doing wrong (in this way, I know that we can run command with oleDb but really don't like this way ...).

Thanks

网友答案:

Based on what you're doing it appears you want that new row to end up in the database. So remove this line:

dataTable.AcceptChanges();

because that's changing the RowState to Unchanged and you need it left at Added. Further, you need to make sure that your adapter has an InsertStatement defined so it can use it.

网友答案:

I would use [] instead of `` but I believe the answer to correct your problem was already given by @Michael Perrenoud

相关阅读:
Top