SqlCommand.Connection Właściwość

Definicja

Pobiera lub ustawia SqlConnection wartość używaną przez to wystąpienie klasy SqlCommand.

public:
 property System::Data::SqlClient::SqlConnection ^ Connection { System::Data::SqlClient::SqlConnection ^ get(); void set(System::Data::SqlClient::SqlConnection ^ value); };
public System.Data.SqlClient.SqlConnection Connection { get; set; }
[System.Data.DataSysDescription("DbCommand_Connection")]
public System.Data.SqlClient.SqlConnection Connection { get; set; }
member this.Connection : System.Data.SqlClient.SqlConnection with get, set
[<System.Data.DataSysDescription("DbCommand_Connection")>]
member this.Connection : System.Data.SqlClient.SqlConnection with get, set
Public Property Connection As SqlConnection

Wartość właściwości

Połączenie ze źródłem danych. Wartość domyślna to null.

Atrybuty

Wyjątki

Właściwość została zmieniona Connection , gdy polecenie zostało wymienione w transakcji.

Przykłady

Poniższy przykład tworzy obiekt SqlCommand i ustawia niektóre z jego właściwości.

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandTimeout = 15;
        command.CommandType = CommandType.Text;
        command.CommandText = queryString;

        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()
        command.Connection = connection
        command.CommandTimeout = 15
        command.CommandType = CommandType.Text
        command.CommandText = queryString

        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

Uwagi

Jeśli polecenie jest wymienione w istniejącej transakcji, a połączenie zostanie zmienione, próba wykonania polecenia zgłosi błąd InvalidOperationException.

Transaction Jeśli właściwość nie ma wartości null i transakcja została już zatwierdzona lub wycofana, Transaction jest ustawiona na wartość null.

Dotyczy

Zobacz też