ADO.Net - SqlCommand Object


SqlCommand - It is an important ADO.Net Object used to send the SQL Data Manipulation and Query Statements from ASP.Net Webpage or Windows forms to the SQL Server Database. We can create and Initialize SqlCommand Object in different Ways. Learn ADO.Net - SqlCommand Object

Find here Sql Command Object


1. It is a ADO.Net Object defined in the System.Data.SqlClient Namespace in the .Net Framework.
2. It is used to send SQL Data Manipulation and Query Statements to SQL Server Database.
3. It is also used to work with Stored Procedure.

In this article I am providing details about how to create and initialize SqlCommand Object in different ways.


Method 1



/*Create and Initialize the SqlConnection Object*/
SqlConnection connectionString = new SqlConnection("[ConnectionString Reference]");

/*Define a Query to Get Records from Employee Table */
string queryString = "SELECT empno, ename, salary, deptno FROM tblEmp";

/* Initialize the SqlCommand Object using createCommand method from the SqlConnection Object */
SqlCommand command =connectionString.CreateCommand();

/*Set the QueryString to the Command Object*/
command.CommandText=queryString;

/*Set the Connection Object Reference */
command.Connection=connectionString;

[Write Query Processing Code Here]


Method 2



/* Create and Initialize the SqlConnection Object*/
SqlConnection connectionString = new SqlConnection("[ConnectionString Reference]");


/*Define a Query to Get Records from Employee Table */
string queryString = "SELECT empno, ename, salary, deptno FROM tblEmp";

/* Initialize the SqlCommand Object using SqlCommand() Constructor */
SqlCommand command = new SqlCommand();

/* Set the QueryString to the Command Object */
command.CommandText=queryString;

/* Set the Connection Object Reference */
command.Connection=connectionString;

[Write Query Processing Code Here]



Method 3



/* Create and Initialize the SqlConnection Object*/
SqlConnection connectionString = new SqlConnection("[ConnectionString Reference]");

//Define a Query to Get Records from Employee Table
string queryString = "SELECT empno, ename, salary, deptno FROM tblEmp";

/* Initialize the SqlCommand Object using SqlCommand(String cmd) Constructor */
SqlCommand command = new SqlCommand(queryString);

/* set the QueryString to the Command Object */
command.CommandText=queryString;

/* Set the Connection Object Reference */
command.Connection=connectionString;

[Write Query Processing Code Here]




Method 4



/* Create and Initialize the SqlConnection Object */
SqlConnection connectionString = new SqlConnection("[ConnectionString Reference]");

/*Define a Query to Get Records from Employee Table */
string queryString = "SELECT empno, ename, salary, deptno FROM tblEmp";

/* Initialize the SqlCommand Object using SqlCommand(String Query, String cmd) Constructor */
SqlCommand command = new SqlCommand(queryString, connectionString);

[Write Query Processing Code Here]


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: