Idioma

SqlCommand Construtores

Definição

Inicializa uma nova instância da classe SqlCommand.

Sobrecargas

Nome Description
SqlCommand()

Inicializa uma nova instância da classe SqlCommand.

SqlCommand(String)

Inicializa uma nova instância da SqlCommand classe com o texto da consulta.

SqlCommand(String, SqlConnection)

Inicializa uma nova instância da SqlCommand classe com o texto da consulta e um SqlConnection.

SqlCommand(String, SqlConnection, SqlTransaction)

Inicializa uma nova instância da SqlCommand classe com o texto da consulta, um SqlConnectione o SqlTransaction.

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

Inicializa uma nova instância da classe com texto SqlCommand de comando, conexão, transação e configuração de criptografia especificados.

SqlCommand()

Origem:
System.Data.SqlClient.notsupported.cs

Inicializa uma nova instância da classe SqlCommand.

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

Exemplos

O exemplo a seguir cria uma SqlCommand propriedade e define a CommandTimeout propriedade.

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 para seus valores padrão. A tabela a seguir mostra valores de propriedade iniciais para uma instância de SqlCommand.

Propriedades Valor inicial
CommandText cadeia de caracteres vazia ("")
CommandTimeout 30
CommandType CommandType.Text
Connection Nulo

Você pode alterar o valor de qualquer uma dessas propriedades por meio de uma chamada separada para a propriedade.

Confira também

Aplica-se a

SqlCommand(String)

Origem:
System.Data.SqlClient.notsupported.cs

Inicializa uma nova instância da SqlCommand classe com o texto da consulta.

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.

Exemplos

O exemplo a seguir cria um SqlCommandtexto de comando de passagem.

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 é criada, as propriedades de SqlCommand leitura/gravação a seguir são definidas como valores iniciais.

Propriedades Valor inicial
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection nulo

Você pode alterar o valor de qualquer uma dessas propriedades por meio de uma chamada separada para a propriedade.

Confira também

Aplica-se a

SqlCommand(String, SqlConnection)

Origem:
System.Data.SqlClient.notsupported.cs

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

connection
SqlConnection

Um SqlConnection que representa a conexão com uma instância de SQL Server.

Exemplos

O exemplo a seguir cria um SqlCommand e define algumas de suas propriedades.

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 valores de propriedade iniciais para uma instância de SqlCommand.

Propriedades Valor inicial
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection Uma nova SqlConnection que é o valor do connection parâmetro.

Você pode alterar o valor de qualquer um desses parâmetros definindo a propriedade relacionada.

Confira também

Aplica-se a

SqlCommand(String, SqlConnection, SqlTransaction)

Origem:
System.Data.SqlClient.notsupported.cs

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

connection
SqlConnection

Um SqlConnection que representa a conexão com uma instância de SQL Server.

transaction
SqlTransaction

A SqlTransaction em que a execução SqlCommand é executada.

Comentários

A tabela a seguir mostra valores de propriedade iniciais para uma instância de SqlCommand.

Propriedades Valor inicial
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection Uma nova SqlConnection que é o valor do connection parâmetro.

Você pode alterar o valor de qualquer um desses parâmetros definindo a propriedade relacionada.

Confira também

Aplica-se a

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

Inicializa uma nova instância da classe com texto SqlCommand de comando, conexão, transação e configuração de criptografia especificados.

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.

connection
SqlConnection

Um SqlConnection que representa a conexão com uma instância de SQL Server.

transaction
SqlTransaction

A SqlTransaction em que a execução SqlCommand é executada.

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

A configuração de criptografia. Para obter mais informações, consulte Always Encrypted.

Aplica-se a