Rediger

Del via


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.

public:
 SqlCommand();
public SqlCommand ();
Public Sub New ()

Examples

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

public void CreateSqlCommand()
{
   SqlCommand command = new SqlCommand();
   command.CommandTimeout = 15;
   command.CommandType = CommandType.Text;
}
Public Sub CreateSqlCommand()
    Dim command As New SqlCommand()
    command.CommandTimeout = 15
    command.CommandType = CommandType.Text
End Sub

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

SqlCommand(String)

Source:
System.Data.SqlClient.notsupported.cs

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

public:
 SqlCommand(System::String ^ cmdText);
public SqlCommand (string cmdText);
new System.Data.SqlClient.SqlCommand : string -> System.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String)

Parameters

cmdText
String

The text of the query.

Examples

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

public void CreateCommand()
 {
    string queryString = "SELECT * FROM Categories ORDER BY CategoryID";
    SqlCommand command = new SqlCommand(queryString);
    command.CommandTimeout = 15;
    command.CommandType = CommandType.Text;
 }
Public Sub CreateCommand()
    Dim queryString As String = "SELECT * FROM Categories ORDER BY CategoryID"
    Dim command As New SqlCommand(queryString)
    command.CommandTimeout = 15
    command.CommandType = CommandType.Text
End Sub

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

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.

public:
 SqlCommand(System::String ^ cmdText, System::Data::SqlClient::SqlConnection ^ connection);
public SqlCommand (string cmdText, System.Data.SqlClient.SqlConnection connection);
new System.Data.SqlClient.SqlCommand : string * System.Data.SqlClient.SqlConnection -> System.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String, connection As SqlConnection)

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.

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]));
        }
    }
}
Public Sub CreateCommand(ByVal queryString As String, _
  ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(queryString, connection)

        connection.Open()
        Dim reader As SqlDataReader = command.ExecuteReader()
            While reader.Read()
            Console.WriteLine(String.Format("{0}, {1}", _
                reader(0), reader(1)))
        End While
    End Using
End Sub

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

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.

public:
 SqlCommand(System::String ^ cmdText, System::Data::SqlClient::SqlConnection ^ connection, System::Data::SqlClient::SqlTransaction ^ transaction);
public SqlCommand (string cmdText, System.Data.SqlClient.SqlConnection connection, System.Data.SqlClient.SqlTransaction transaction);
new System.Data.SqlClient.SqlCommand : string * System.Data.SqlClient.SqlConnection * System.Data.SqlClient.SqlTransaction -> System.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String, connection As SqlConnection, transaction As SqlTransaction)

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

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

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

public:
 SqlCommand(System::String ^ cmdText, System::Data::SqlClient::SqlConnection ^ connection, System::Data::SqlClient::SqlTransaction ^ transaction, System::Data::SqlClient::SqlCommandColumnEncryptionSetting columnEncryptionSetting);
public SqlCommand (string cmdText, System.Data.SqlClient.SqlConnection connection, System.Data.SqlClient.SqlTransaction transaction, System.Data.SqlClient.SqlCommandColumnEncryptionSetting columnEncryptionSetting);
new System.Data.SqlClient.SqlCommand : string * System.Data.SqlClient.SqlConnection * System.Data.SqlClient.SqlTransaction * System.Data.SqlClient.SqlCommandColumnEncryptionSetting -> System.Data.SqlClient.SqlCommand
Public Sub New (cmdText As String, connection As SqlConnection, transaction As SqlTransaction, columnEncryptionSetting As SqlCommandColumnEncryptionSetting)

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