I have an application with an editor of multiple DataTables, that are stored in a DataSet wich I load through a DataAdapter. My objective is to update the database with all the changes in all of the tables. I thought of using the same DataAdapter used to load the data with a command builder, just as if it were a single DataTable.
This is how I load the DataSet.
using (SqlConnection con = new SqlConnection(ConnectionString)) {
con.Open();
var query = "SELECT * FROM Parameters;
SELECT * FROM Status;
SELECT * FROM Commands;";
var selectCommand = new SqlCommand(query, con);
DataAdapter = new SqlDataAdapter(selectCommand);
DataAdapter.AcceptChangesDuringFill =
DataAdapter.AcceptChangesDuringUpdate = true;
DataAdapter.TableMappings.Add("Table", "Parameters");
DataAdapter.TableMappings.Add("Table1", "Status");
DataAdapter.TableMappings.Add("Table2", "Commands");
DataAdapter.Fill(DataSet);
}
After the user edits the data in the various tables an update is called, in wich I use the same DataAdapter with the SELECT query used to load the data in the first place and a CommandBuilder. The problem that I have is that doing like this the command builder “detects” just the first table and create the queries for the update just for said table.
using (SqlConnection con = new SqlConnection(ConnectionString)) {
con.Open();
try {
DataAdapter.SelectCommand.Connection = con;
SqlCommandBuilder builder = new(DataAdapter);
DataAdapter.Update(DataSet);
}
catch (SqlException) {
}
}
Is there a possibility to make this work such as I intended or do I have to change the approach?
Samuel Bargelli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.