Programming Newbie
Programming Newbie

Reputation: 1227

Confusion concerning OleDbCommand

I know that to instantiate a new OLEDbCommand object you do this:

OleDbCommand command = new OleDbCommand(queryString, connection);

However, I am confused as to what this line of code is doing:

OleDbCommand cmd = aConnection.CreateCommand();

I know that aConnection is an OleDbConnection object which was instantiated earlier in the code.

From the MSDN Library I know that CreateCommand() is an OdbcCommand object associated with the OdbcConnection. However, the Library doesn't really go any further to explain its purpose.

Even though I know what the individual components are, I am unsure of what is happening here. Is cmd an OleDbCommand object that is being instantiated in order to be able to use SQL commands? I infer this because further on in the code there is the following:

cmd.CommandText = "SELECT * FROM Team where typeOfSport = '" + typeOfSport + "'";

Upvotes: 0

Views: 101

Answers (2)

Yahia
Yahia

Reputation: 70369

According to MSDN aConnection.CreateCommand():

Creates and returns an OleDbCommand object associated with the OleDbConnection.

This basically is a convenience method which already sets up the respective connection on the command without setting up the CommandText - thus could be used as a utility method whenever you need a OleDbCommand object without supplying the CommandText directly but later on...

Upvotes: 2

Gratzy
Gratzy

Reputation: 9389

It is just creating a command from the connection object. The command will be associated with the connection it was created from.

OleDbCommand command = new OleDbCommand(queryString, connection);

is the same as

OleDbCommand command = connection.CreateCommand()

Upvotes: 0

Related Questions