Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to:
.NET Framework
.NET
.NET Standard
The Microsoft SqlClient Data Provider for SQL Server has SqlCommand object that inherits from DbCommand. This object exposes methods for executing commands based on the type of command and desired return value, as described in the following table.
Command | Return Value |
---|---|
ExecuteReader |
Returns a DataReader object. |
ExecuteScalar |
Returns a single scalar value. |
ExecuteNonQuery |
Executes a command that does not return any rows. |
ExecuteXMLReader |
Returns an XmlReader. Available for a SqlCommand object only. |
Each strongly typed command object also supports a CommandType enumeration that specifies how a command string is interpreted, as described in the following table.
CommandType | Description |
---|---|
Text |
A SQL command defining the statements to be executed at the data source. |
StoredProcedure |
The name of the stored procedure. You can use the Parameters property of a command to access input and output parameters and return values, regardless of which Execute method is called. |
TableDirect |
The name of a table. |
Important
When using ExecuteReader
, return values and output parameters will not be accessible until the DataReader
is closed.
The following code example demonstrates how to create a SqlCommand object to execute a stored procedure by setting its properties. A SqlParameter object is used to specify the input parameter to the stored procedure. The command is executed using the ExecuteReader method, and the output from the SqlDataReader is displayed in the console window.
static void GetSalesByCategory(string connectionString,
string categoryName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Create the command and set its properties.
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SalesByCategory";
command.CommandType = CommandType.StoredProcedure;
// Add the input parameter and set its properties.
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@CategoryName";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = categoryName;
// Add the parameter to the Parameters collection.
command.Parameters.Add(parameter);
// Open the connection and execute the reader.
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
}
}
The Microsoft SqlClient Data Provider for SQL Server adds diagnostic counters to enable you to detect intermittent problems related to failed command executions. For more information, see Diagnostic counters in SqlClient.
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest SQL, Fabric and Power BI learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayTraining
Module
Create stored procedures and user-defined functions - Training
This content is a part of Create stored procedures and user-defined functions.
Documentation
Configuring parameters - ADO.NET Provider for SQL Server
Command objects use parameters to pass values to SQL statements or stored procedures, providing type checking and validation in ADO.NET.
Modify data with stored procedures - ADO.NET Provider for SQL Server
Describes how to use stored procedure input parameters and output parameters to insert a row in a database, returning a new identity value.
Generating commands with CommandBuilders - ADO.NET Provider for SQL Server
Explains how to use command builders to automatically generate INSERT, UPDATE, and DELETE commands for a `DataAdapter` that has a single-table SELECT command.