adonet
  1. adonet-commands

Commands - (ADO.NET Connection)

Commands in ADO.NET are used to execute SQL statements and stored procedures against a database. They can also be used to pass parameters and return values to and from a database. Commands are created using the SqlCommand class in the System.Data.SqlClient namespace.

Syntax

Here's the basic syntax for creating a SqlCommand object:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand(sql, connection);
}

SqlConnection is the class used to create a connection to a database.

  • SqlCommand is the class used to execute SQL statements against a database.
  • connectionString is a string that contains the connection information, such as the database name, server name, and authentication details.
  • sql is the SQL statement or stored procedure that you want to execute.

Example

Here's an example of how to create and execute a SqlCommand object:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand("SELECT * FROM Customers WHERE Country = @Country", connection);
    command.Parameters.AddWithValue("@Country", "USA");

    connection.Open();
    SqlDataReader reader = command.ExecuteReader();

    while (reader.Read())
    {
        Console.WriteLine(reader["CompanyName"]);
    }

    reader.Close();
}

This example creates a SqlCommand object that selects all the customers from the "Customers" table where the country is "USA". It then adds a parameter to the command to specify the country value. Finally, it executes the command and reads the results using a SqlDataReader.

Output

The output of the example above is a list of company names for all customers located in the USA.

Alfreds Futterkiste
Ana Trujillo Emparedados y helados
Antonio Moreno Taquería
Around the Horn
Blauer See Delikatessen
...

Explanation

The SqlCommand class is used to execute SQL statements or stored procedures against a database. It supports the execution of parameterized queries, which can be used to pass values to and from a database. When you execute a command, it returns a SqlDataReader object, which provides access to the results of the query.

Use

The SqlCommand class is used to execute SQL statements and stored procedures against a database. It can be used to perform a variety of tasks, including:

  • Retrieving data from a database
  • Updating data in a database
  • Inserting data into a database
  • Deleting data from a database
  • Executing stored procedures

Important Points

  • The SqlCommand class is used to execute SQL statements and stored procedures against a database.
  • The SqlCommand class supports the execution of parameterized queries.
  • The SqlCommand class returns a SqlDataReader object, which provides access to the results of the query.

Summary

In this page, we discussed the basics of commands in ADO.NET. We covered the syntax, example, output, explanation, use, and important points of commands. The SqlCommand class is a powerful tool for executing SQL statements and stored procedures against a database in ADO.NET. It can be used for a variety of tasks, such as retrieving and updating data, inserting and deleting data, and executing stored procedures.

Published on: