As the result of an operation, there are bunch of new datarows for several tables in our database. Referential integrity is ensured by using unique negative numbers in datarows for the primary-keys and foreign-keys.
I have written the example below to simulate the situation:
using (var con = new SqlConnection("ConnectionString"))
{
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Users where 1=0;SELECT * FROM Address where 1=0;", con))
{
adapter.TableMappings.Add("Table", "Users");
adapter.TableMappings.Add("Table1", "Address");
var ds = new DataSet();
using (var commandBuilder = new SqlCommandBuilder(adapter))
{
adapter.Fill(ds);
//--------------------
var userRow = ds.Tables["Users"].NewRow();
var addressRow = ds.Tables["Address"].NewRow();
//userRow
userRow["UserId"] = -1;
userRow["FirstName"] = "F1";
userRow["LastName"] = "L1";
//addressRow
addressRow["AddressId"] = -2;
addressRow["UserId"] = -1;
addressRow["Street"] = "S1";
ds.Tables["Users"].Rows.Add(userRow);
ds.Tables["Address"].Rows.Add(addressRow);
adapter.Update(ds);
}
}
}
But this only insert the rows in Users table since the first select query belongs to this table.
Any idea or solution about this requirement?