Use DataTable Update() Method to Update Multiple SQL Tables?

2.1k views Asked by At

I'm trying to update my SQL backend with data that has changed in my webform.

That data is initially fetched from SQL like so

string cs = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection connection = new SqlConnection(cs);
SqlCommand command = new SqlCommand("my.storedprocedure", cs);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@customerID", custid));

command.CommandTimeout = 300;

connection.Open();
SqlDataAdapter sda = new SqlDataAdapter(command);
connection.Close();

sda.FillSchema(myDataTable, SchemaType.Source);

sda.Fill(myDataTable);

The stored procedure makes a selection

SELECT c.*
,cs.*
,p.*
,sl.*
,drl.*
FROM tableC as c
INNER JOIN tableCS as cs ON c.[custID] = cs.[custID]
INNER JOIN tableSL as sl ON cs.[custStatusID] = sl.[custStatusID]
LEFT OUTER JOIN tableP as p ON c.custID = p.custID
LEFT OUTER JOIN tableDRL as drl ON cs.custID = drl.custID
WHERE c.[custID] = @custID AND c.[EndDate] IS NULL AND cs.[EndDate] IS NULL

I then manipulate the data table that is filled with this stored procedure, call a dt.NewRow(), add values to that new row, add the row into the data table. Set fields in the previous row, etc.

My question is, when I get ready to call a SqlDataAdapter.Update(myDataTable) will the data adapter go ahead and make the update to all the tables I joined too? Based on what's in my data table?

1

There are 1 answers

0
William Xifaras On

The short answer is no - a single adapter is for a single table. You need to use a DataSet if you are using ADO.NET.

Using a DataSet:

Populating a DataSet from a DataAdapter

ADO.NET - Updating Multiple DataTables

Fill DataSet with multiple Tables and update them with DataAdapter

Using TableAdapterManager:

TableAdapterManager Overview