0

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?

Mori
  • 2,484
  • 5
  • 28
  • 45
  • I think you gonna need multiple `SqlDataAdapter` for this. Please refer; http://stackoverflow.com/questions/4574606/c-sharp-dataadapter-and-dataset-with-multiple-table – Irshad Jan 08 '16 at 10:16
  • @Irshad We are not using typed-dataset. If I could not figure out a solution, I may have to turn it to a typed-dataset approach. – Mori Jan 08 '16 at 10:50

0 Answers0