1

I am not sure about these 2 things what the difference between using connection and command object when insert, update and delete which is called stored procedure from SQL Server.

For example about connection:

Cn.Execute "Exec ProcedureName" 

about command obj:

Cmd.CommandType=AdCmdStoredProc
CmdCommandText="....."
Cmd.ActiveConnection=Cn
Cmd.Parameters.Append .....
........

I really don't know when to use each of them because they look similar.

Thanks in advance

1 Answer 1

4

There are so many articles available, which explains the use of Command and Connection object in Ado.Net. Few of them are as below:

http://www.c-sharpcorner.com/UploadFile/c5c6e2/working-with-command-object/

http://www.c-sharpcorner.com/uploadfile/mahesh/connection-object-in-ado-net/

http://csharp.net-informations.com/data-providers/csharp-ado.net-connection.htm

Connection Object: Connection object is the basic Ado.Net component used to create a link between your application to your data source. So you define a connection string which you can use to initialize a connection to your datasource.

Command Object: Command object is another Ado.Net component used to execute queries against your datasource using connection object. So basically you need connection and command object to execute queries on your datasource. Using Command object you can execute inline queries, stored procedure, etc.

A sample code:

        SqlConnection con = new SqlConnection(connectionString); // creates object of connection and pass the connection string to the constructor
        SqlCommand cmd = new SqlCommand(); // creates object of command
        cmd.Connection = con; // tells command object about connection, where the query should be fired
        cmd.CommandText = "Query"; // your query will go here
        cmd.CommandType = System.Data.CommandType.Text; // it tells the command type which can be text, stored procedure

        con.Open(); // opens the connection to datasource
        var result = cmd.ExecuteReader(); // executes the query on datasource using command object
        con.Close(); // closes the connection

I hope this will help you. :)

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

2 Comments

The code in the OP looks like classic ADO rather than ADO.NET
Yes, basically the code is same in both ADO and ADO.Net, the difference is performance, and the way they work in background.

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.