0

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?

3 Answers 3

1

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):

// Fields
private SqlCommandSet _commandSet;
private SqlCommand _deleteCommand;
private SqlCommand _insertCommand;
private SqlCommand _selectCommand;
private int _updateBatchSize;
private SqlCommand _updateCommand;
private static readonly object EventRowUpdated;
private static readonly object EventRowUpdating;

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanky you Mark. This actually explains it. Do you believe is wrong then using the same connection created by the select command for the insert command if I'm using the same database. If not, maybe I don't understand how connections in a database work. :)
@user990692: Using the same connection for all of the commands is the best practice in most situations (e.g., if running under a transaction, you would want them all using the same connection almost certainly).
0

It's the parameter needed by SqlCommand constructor, AFAIR InsertCommand could be created automatically from select command then you don't need this duplication even for defining the InsertCommand.

From MSDN

if primary key information is present in the DataSet, the InsertCommand can be generated automatically if you set the SelectCommand property and use the SqlCommandBuilder.

1 Comment

Yeah I know about the SqlCommandBuilder but in this case I need to create my own insert cause I'm using multiple tables in the select command. What I did was I assign the Connection property of the select command to the Connection property of insert command. I don't know if this is a good approach or not tho
0

as MSDN states it SqlDataAdapter

the InsertCommand can be generated automatically if you set the SelectCommand property and use the SqlCommandBuilder.

When InsertCommand is assigned to a previously created SqlCommand, the SqlCommand is not cloned. The InsertCommand maintains a reference to the previously created SqlCommand object.

Ecah command object, associated with connection, so every time it needs info about the command object to perform operation.

1 Comment

I get that. However, my issue is not the SqlCommand but the connection. When you create the adapter you pass the connection or the connection string that the adapter will use. After this I assume that the adapter knows to which connection to refer to, right? So again, if I need to create my insert command for whatever reason why do I need to pass a new connection or a connection if it was already created when calling the SqlDataAdapter constructor. Just saying shouldnt be a better design then doing it this way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.