Freigeben über


SqlCommand.Connection-Eigenschaft

Ruft die SqlConnection ab, die von dieser SqlCommand-Instanz verwendet wird, oder legt diese fest.

Namespace: System.Data.SqlClient
Assembly: System.Data (in system.data.dll)

Syntax

'Declaration
Public Property Connection As SqlConnection
'Usage
Dim instance As SqlCommand
Dim value As SqlConnection

value = instance.Connection

instance.Connection = value
public SqlConnection Connection { get; set; }
public:
property SqlConnection^ Connection {
    SqlConnection^ get ();
    void set (SqlConnection^ value);
}
/** @property */
public SqlConnection get_Connection ()

/** @property */
public void set_Connection (SqlConnection value)
public function get Connection () : SqlConnection

public function set Connection (value : SqlConnection)

Eigenschaftenwert

Die Verbindung mit einer Datenquelle. Der Standardwert ist NULL (Nothing in Visual Basic).

Ausnahmen

Ausnahmetyp Bedingung

InvalidOperationException

Die Connection-Eigenschaft wurde während der Ausführung einer Transaktion geändert.

Hinweise

Wenn Sie Connection festlegen, während eine Transaktion ausgeführt wird und die Transaction-Eigenschaft nicht NULL ist, wird eine InvalidOperationException ausgelöst. Wenn die Transaction-Eigenschaft nicht NULL ist und für die Transaktion bereits ein Commit oder ein Rollback ausgeführt wurde, wird Transaction auf NULL festgelegt.

Beispiel

Im folgenden Beispiel wird ein SqlCommand erstellt, und einige seiner Eigenschaften werden festgelegt.

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
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]));
        }
    }
}

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

SqlCommand-Klasse
SqlCommand-Member
System.Data.SqlClient-Namespace
SqlCommand.CommandText-Eigenschaft
SqlCommand.CommandTimeout-Eigenschaft
SqlCommand.CommandType-Eigenschaft