SqlCommand.Connection Özellik

Tanım

SqlConnectionBu örneği tarafından kullanılan öğesini alır veya ayarlar SqlCommand .Gets or sets the SqlConnection used by this instance of the 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

Özellik Değeri

SqlConnection

Bir veri kaynağıyla bağlantı.The connection to a data source. Varsayılan değer: null.The default value is null.

Öznitelikler

Özel durumlar

ConnectionKomut bir işlemde kayıtlı durumdayken özellik değiştirildi.The Connection property was changed while the command was enlisted in a transaction.

Örnekler

Aşağıdaki örnek bir oluşturur SqlCommand ve bazı özelliklerini ayarlar.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();
        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

Açıklamalar

Komut var olan bir işlemde kayıtlı ise ve bağlantı değiştirilirse, komutu yürütmeye çalışmak bir oluşturur InvalidOperationException .If the command is enlisted in an existing transaction, and the connection is changed, trying to execute the command will throw an InvalidOperationException.

TransactionÖzellik null değilse ve işlem zaten kaydedilmiş veya geri alınırsa, Transaction null olarak ayarlanır.If the Transaction property is not null and the transaction has already been committed or rolled back, Transaction is set to null.

Şunlara uygulanır

Ayrıca bkz.