If I create a
SqlDataAdapter adapterSales = new SqlDataAdapter (mySelectCommand, connString)
and then I create the insert command for the adapter lets say
adapterSales.InsertCommand = new SqlCommand (myInsertCommand);
Why do I need to pass another connection to the last constructor? I mean if I already passed the connection string why the adapter doesn’t work with that to create the sql connection that I’m going to be working with?
It is the command objects (select, insert, update, delete) that need the connection. The data adapter itself doesn’t actually have a specific connection directly associated with it. Using Reflector, I see that the following are the members of
SqlDataAdapter(no connection object directly in it):So, in theory, you could use a different connection with each command associated with the data adapter, but it is not obvious that there would be many uses for that scenario (maybe dealing with two database where one is read only and updates are sent to the other).