다음을 통해 공유


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

설명

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

속성이 Transaction null이 아니고 트랜잭션이 이미 커밋되었거나 롤백 Transaction 된 경우 null로 설정됩니다.

적용 대상

추가 정보