SqlCommand Construtores

Definição

Inicializa uma nova instância da classe SqlCommand.Initializes a new instance of the SqlCommand class.

Sobrecargas

SqlCommand()

Inicializa uma nova instância da classe SqlCommand.Initializes a new instance of the SqlCommand class.

SqlCommand(String)

Inicializa uma nova instância da classe SqlCommand com o texto da consulta.Initializes a new instance of the SqlCommand class with the text of the query.

SqlCommand(String, SqlConnection)

Inicializa uma nova instância da classe SqlCommand com o texto da consulta e um SqlConnection.Initializes a new instance of the SqlCommand class with the text of the query and a SqlConnection.

SqlCommand(String, SqlConnection, SqlTransaction)

Inicializa uma nova instância da classe SqlCommand com o texto da consulta, um SqlConnection e o 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)

Inicializa uma nova instância da classe SqlCommand com texto de comando especificado, conexões, transações e configuração de criptografia.Initializes a new instance of the SqlCommand class with specified command text, connection, transaction, and encryption setting.

SqlCommand()

Inicializa uma nova instância da classe SqlCommand.Initializes a new instance of the SqlCommand class.

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

Exemplos

O exemplo a seguir cria um SqlCommand e define a CommandTimeout propriedade.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

Comentários

O construtor base inicializa todos os campos com seus valores padrão.The base constructor initializes all fields to their default values. A tabela a seguir mostra os valores de propriedade inicial para uma instância do SqlCommand .The following table shows initial property values for an instance of SqlCommand.

PropriedadesProperties Valor inicialInitial value
CommandText Cadeia de caracteres vazia ("")empty string ("")
CommandTimeout 3030
CommandType CommandType.Text
Connection NuloNull

Você pode alterar o valor de qualquer uma dessas propriedades por meio de uma chamada separada para a propriedade.You can change the value for any of these properties through a separate call to the property.

Aplica-se a

SqlCommand(String)

Inicializa uma nova instância da classe SqlCommand com o texto da consulta.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)

Parâmetros

cmdText
String

O texto da consulta.The text of the query.

Exemplos

O exemplo a seguir cria um SqlCommand , passando a cadeia de conexão e o texto do comando.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 

Comentários

Quando uma instância do SqlCommand é criada, as seguintes propriedades de leitura/gravação são definidas como valores iniciais.When an instance of SqlCommand is created, the following read/write properties are set to initial values.

PropriedadesProperties Valor inicialInitial value
CommandText cmdText
CommandTimeout 3030
CommandType CommandType.Text
Connection nullnull

Você pode alterar o valor de qualquer uma dessas propriedades por meio de uma chamada separada para a propriedade.You can change the value for any of these properties through a separate call to the property.

Aplica-se a

SqlCommand(String, SqlConnection)

Inicializa uma nova instância da classe SqlCommand com o texto da consulta e um SqlConnection.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)

Parâmetros

cmdText
String

O texto da consulta.The text of the query.

connection
SqlConnection

Um SqlConnection que representa a conexão com uma instância do SQL Server.A SqlConnection that represents the connection to an instance of SQL Server.

Exemplos

O exemplo a seguir cria um SqlCommand e define algumas de suas propriedades.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

Comentários

A tabela a seguir mostra os valores de propriedade inicial para uma instância do SqlCommand .The following table shows initial property values for an instance of SqlCommand.

PropriedadesProperties Valor inicialInitial value
CommandText cmdText
CommandTimeout 3030
CommandType CommandType.Text
Connection Um novo SqlConnection que é o valor para o connection parâmetro.A new SqlConnection that is the value for the connection parameter.

Você pode alterar o valor de qualquer um desses parâmetros definindo a propriedade related.You can change the value for any of these parameters by setting the related property.

Aplica-se a

SqlCommand(String, SqlConnection, SqlTransaction)

Inicializa uma nova instância da classe SqlCommand com o texto da consulta, um SqlConnection e o SqlTransaction.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)

Parâmetros

cmdText
String

O texto da consulta.The text of the query.

connection
SqlConnection

Um SqlConnection que representa a conexão com uma instância do SQL Server.A SqlConnection that represents the connection to an instance of SQL Server.

transaction
SqlTransaction

O SqlTransaction no qual o SqlCommand é executado.The SqlTransaction in which the SqlCommand executes.

Comentários

A tabela a seguir mostra os valores de propriedade inicial para uma instância do SqlCommand .The following table shows initial property values for an instance of SqlCommand.

PropriedadesProperties Valor inicialInitial value
CommandText cmdText
CommandTimeout 3030
CommandType CommandType.Text
Connection Um novo SqlConnection que é o valor para o connection parâmetro.A new SqlConnection that is the value for the connection parameter.

Você pode alterar o valor de qualquer um desses parâmetros definindo a propriedade related.You can change the value for any of these parameters by setting the related property.

Aplica-se a

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

Inicializa uma nova instância da classe SqlCommand com texto de comando especificado, conexões, transações e configuração de criptografia.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)

Parâmetros

cmdText
String

O texto da consulta.The text of the query.

connection
SqlConnection

Um SqlConnection que representa a conexão com uma instância do SQL Server.A SqlConnection that represents the connection to an instance of SQL Server.

transaction
SqlTransaction

O SqlTransaction no qual o SqlCommand é executado.The SqlTransaction in which the SqlCommand executes.

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

A configuração de criptografia.The encryption setting. Para obter mais informações, consulte Always Encrypted.For more information, see Always Encrypted.

Aplica-se a