SqlCommand Konstruktory

Definice

Inicializuje novou instanci SqlCommand třídy .

Přetížení

SqlCommand()

Inicializuje novou instanci SqlCommand třídy .

SqlCommand(String)

Inicializuje novou instanci SqlCommand třídy s textem dotazu.

SqlCommand(String, SqlConnection)

Inicializuje novou instanci SqlCommand třídy s textem dotazu a objektem SqlConnection.

SqlCommand(String, SqlConnection, SqlTransaction)

Inicializuje novou instanci SqlCommand třídy s textem dotazu , SqlConnectiona SqlTransaction.

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

Inicializuje novou instanci SqlCommand třídy se zadaným textem příkazu, připojením, transakcí a šifrování nastavení.

SqlCommand()

Inicializuje novou instanci SqlCommand třídy .

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

Příklady

Následující příklad vytvoří SqlCommand a nastaví CommandTimeout vlastnost .

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

Poznámky

Základní konstruktor inicializuje všechna pole na výchozí hodnoty. Následující tabulka uvádí počáteční hodnoty vlastností pro instanci .SqlCommand

Vlastnosti Počáteční hodnota
CommandText prázdný řetězec ("")
CommandTimeout 30
CommandType CommandType.Text
Connection Null

Hodnotu kterékoli z těchto vlastností můžete změnit pomocí samostatného volání vlastnosti .

Viz také

Platí pro

SqlCommand(String)

Inicializuje novou instanci SqlCommand třídy s textem dotazu.

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)

Parametry

cmdText
String

Text dotazu

Příklady

Následující příklad vytvoří a SqlCommandpředá připojovací řetězec a text příkazu.

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

Poznámky

Při vytvoření instance objektu SqlCommand jsou následující vlastnosti pro čtení a zápis nastaveny na počáteční hodnoty.

Vlastnosti Počáteční hodnota
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection null

Hodnotu kterékoli z těchto vlastností můžete změnit pomocí samostatného volání vlastnosti .

Viz také

Platí pro

SqlCommand(String, SqlConnection)

Inicializuje novou instanci SqlCommand třídy s textem dotazu a objektem 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)

Parametry

cmdText
String

Text dotazu

connection
SqlConnection

ASqlConnection, který představuje připojení k instanci SQL Server.

Příklady

Následující příklad vytvoří SqlCommand a nastaví některé jeho vlastnosti.

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

Poznámky

Následující tabulka uvádí počáteční hodnoty vlastností pro instanci .SqlCommand

Vlastnosti Počáteční hodnota
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection Nový SqlConnection , který je hodnotou parametru connection .

Hodnotu kteréhokoli z těchto parametrů můžete změnit nastavením související vlastnosti.

Viz také

Platí pro

SqlCommand(String, SqlConnection, SqlTransaction)

Inicializuje novou instanci SqlCommand třídy s textem dotazu , SqlConnectiona 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)

Parametry

cmdText
String

Text dotazu

connection
SqlConnection

ASqlConnection, který představuje připojení k instanci SQL Server.

transaction
SqlTransaction

Hodnota SqlTransaction , ve které se SqlCommand provede.

Poznámky

Následující tabulka uvádí počáteční hodnoty vlastností pro instanci .SqlCommand

Vlastnosti Počáteční hodnota
CommandText cmdText
CommandTimeout 30
CommandType CommandType.Text
Connection Nový SqlConnection , který je hodnotou parametru connection .

Hodnotu kteréhokoli z těchto parametrů můžete změnit nastavením související vlastnosti.

Viz také

Platí pro

SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

Inicializuje novou instanci SqlCommand třídy se zadaným textem příkazu, připojením, transakcí a šifrování nastavení.

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)

Parametry

cmdText
String

Text dotazu

connection
SqlConnection

ASqlConnection, který představuje připojení k instanci SQL Server.

transaction
SqlTransaction

Hodnota SqlTransaction , ve které se SqlCommand provede.

columnEncryptionSetting
SqlCommandColumnEncryptionSetting

Nastavení šifrování. Další informace najdete v tématu Always Encrypted.

Platí pro