SqlCommand.Connection 속성

정의

SqlCommand의 이 인스턴스에서 사용되는 SqlConnection을 가져오거나 설정합니다.

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

속성 값

데이터 원본에 대한 연결입니다. 기본값은 null입니다.

특성

예외

명령이 트랜잭션에 참여하는 동안 Connection 속성이 변경되었습니다.

예제

다음 예에서는 을 SqlCommand 만들고 일부 속성을 설정합니다.

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

설명

명령이 기존 트랜잭션에 등록되어 있고 연결이 변경되면 명령을 실행하려고 하면 가 InvalidOperationExceptionthrow됩니다.

Transaction 속성이 null이 아니고 트랜잭션이 이미 커밋 또는 롤백된 경우에는 Transaction이 null로 설정됩니다.

적용 대상

추가 정보