SqlCommand Constructors

Definition

Initializes a new instance of the SqlCommand class.

Overloads

SqlCommand()

Initializes a new instance of the SqlCommand class.

SqlCommand(String)

Initializes a new instance of the SqlCommand class with the text of the query.

SqlCommand(String, SqlConnection)

Initializes a new instance of the SqlCommand class with the text of the query and a SqlConnection.

SqlCommand(String, SqlConnection, SqlTransaction)

Initializes a new instance of the SqlCommand class with the text of the query, a SqlConnection, and the SqlTransaction.

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

Initializes a new instance of the SqlCommand class with specified command text, connection, transaction, and encryption setting.

SqlCommand()

Source:
System.Data.SqlClient.notsupported.cs

Initializes a new instance of the SqlCommand class.

C#
public SqlCommand();

Examples

The following example creates a SqlCommand and sets the CommandTimeout property.

C#
public void CreateSqlCommand()
{
   SqlCommand command = new SqlCommand();
   command.CommandTimeout = 15;
   command.CommandType = CommandType.Text;
}

Remarks

The base constructor initializes all fields to their default values. The following table shows initial property values for an instance of SqlCommand.

Properties Initial value
CommandText empty string ("")
CommandTimeout 30
CommandType CommandType.Text
Connection Null

You can change the value for any of these properties through a separate call to the property.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SqlCommand(String)

Source:
System.Data.SqlClient.notsupported.cs

Initializes a new instance of the SqlCommand class with the text of the query.

C#
public SqlCommand(string cmdText);

Parameters

cmdText
String

The text of the query.

Examples

The following example creates a SqlCommand, passing in the connection string and command text.

C#
public void CreateCommand()
 {
    string queryString = "SELECT * FROM Categories ORDER BY CategoryID";
    SqlCommand command = new SqlCommand(queryString);
    command.CommandTimeout = 15;
    command.CommandType = CommandType.Text;
 }

Remarks

When an instance of SqlCommand is created, the following read/write properties are set to initial values.

Properties Initial value
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection null

You can change the value for any of these properties through a separate call to the property.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SqlCommand(String, SqlConnection)

Source:
System.Data.SqlClient.notsupported.cs

Initializes a new instance of the SqlCommand class with the text of the query and a SqlConnection.

C#
public SqlCommand(string cmdText, System.Data.SqlClient.SqlConnection connection);

Parameters

cmdText
String

The text of the query.

connection
SqlConnection

A SqlConnection that represents the connection to an instance of SQL Server.

Examples

The following example creates a SqlCommand and sets some of its properties.

C#
private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(
            queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}, {1}",
                reader[0], reader[1]));
        }
    }
}

Remarks

The following table shows initial property values for an instance of SqlCommand.

Properties Initial value
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection A new SqlConnection that is the value for the connection parameter.

You can change the value for any of these parameters by setting the related property.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SqlCommand(String, SqlConnection, SqlTransaction)

Source:
System.Data.SqlClient.notsupported.cs

Initializes a new instance of the SqlCommand class with the text of the query, a SqlConnection, and the SqlTransaction.

C#
public SqlCommand(string cmdText, System.Data.SqlClient.SqlConnection connection, System.Data.SqlClient.SqlTransaction transaction);

Parameters

cmdText
String

The text of the query.

connection
SqlConnection

A SqlConnection that represents the connection to an instance of SQL Server.

transaction
SqlTransaction

The SqlTransaction in which the SqlCommand executes.

Remarks

The following table shows initial property values for an instance of SqlCommand.

Properties Initial value
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection A new SqlConnection that is the value for the connection parameter.

You can change the value for any of these parameters by setting the related property.

See also

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6 (package-provided), 7 (package-provided), 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

Initializes a new instance of the SqlCommand class with specified command text, connection, transaction, and encryption setting.

C#
public SqlCommand(string cmdText, System.Data.SqlClient.SqlConnection connection, System.Data.SqlClient.SqlTransaction transaction, System.Data.SqlClient.SqlCommandColumnEncryptionSetting columnEncryptionSetting);

Parameters

cmdText
String

The text of the query.

connection
SqlConnection

A SqlConnection that represents the connection to an instance of SQL Server.

transaction
SqlTransaction

The SqlTransaction in which the SqlCommand executes.

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

The encryption setting. For more information, see Always Encrypted.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1