SqlCommand.Parameters 속성

정의

SqlParameterCollection를 가져옵니다.

public:
 property System::Data::SqlClient::SqlParameterCollection ^ Parameters { System::Data::SqlClient::SqlParameterCollection ^ get(); };
public System.Data.SqlClient.SqlParameterCollection Parameters { get; }
[System.Data.DataSysDescription("DbCommand_Parameters")]
public System.Data.SqlClient.SqlParameterCollection Parameters { get; }
member this.Parameters : System.Data.SqlClient.SqlParameterCollection
[<System.Data.DataSysDescription("DbCommand_Parameters")>]
member this.Parameters : System.Data.SqlClient.SqlParameterCollection
Public ReadOnly Property Parameters As SqlParameterCollection

속성 값

Transact-SQL 문이나 저장 프로시저의 매개 변수입니다. 기본값은 빈 컬렉션입니다.

특성

예제

다음 예제에서는 를 만들고 SqlCommand 매개 변수를 에 SqlParameterCollection추가하는 방법을 보여 줍니다.

private static void UpdateDemographics(Int32 customerID,
    string demoXml, string connectionString)
{
    // Update the demographics for a store, which is stored
    // in an xml column.
    string commandText = "UPDATE Sales.Store SET Demographics = @demographics "
        + "WHERE CustomerID = @ID;";

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(commandText, connection);
        command.Parameters.Add("@ID", SqlDbType.Int);
        command.Parameters["@ID"].Value = customerID;

        // Use AddWithValue to assign Demographics.
        // SQL Server will implicitly convert strings into XML.
        command.Parameters.AddWithValue("@demographics", demoXml);

        try
        {
            connection.Open();
            Int32 rowsAffected = command.ExecuteNonQuery();
            Console.WriteLine("RowsAffected: {0}", rowsAffected);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}
Private Sub UpdateDemographics(ByVal customerID As Integer, _
    ByVal demoXml As String, _
    ByVal connectionString As String)

    ' Update the demographics for a store, which is stored 
    ' in an xml column.
    Dim commandText As String = _
     "UPDATE Sales.Store SET Demographics = @demographics " _
     & "WHERE CustomerID = @ID;"

    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(commandText, connection)

        ' Add CustomerID parameter for WHERE clause.
        command.Parameters.Add("@ID", SqlDbType.Int)
        command.Parameters("@ID").Value = customerID

        ' Use AddWithValue to assign Demographics.
        ' SQL Server will implicitly convert strings into XML.
        command.Parameters.AddWithValue("@demographics", demoXml)

        Try
            connection.Open()
            Dim rowsAffected As Integer = command.ExecuteNonQuery()
            Console.WriteLine("RowsAffected: {0}", rowsAffected)

        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Using
End Sub

설명

SQL Server Microsoft .NET Framework 데이터 공급자는 매개 변수를 SQL 문 또는 명령CommandType.Text으로 호출된 저장 프로시저에 전달하기 위한 물음표(?) 자리 표시자를 지원하지 않습니다. 이 경우 명명 된 매개 변수를 사용 합니다. 예를 들면 다음과 같습니다.

SELECT * FROM Customers WHERE CustomerID = @CustomerID

참고

컬렉션의 매개 변수가 실행할 쿼리의 요구 사항과 일치하지 않으면 오류가 발생할 수 있습니다.

자세한 내용은 매개 변수 및 매개 변수 데이터 형식 구성을 참조하세요.

적용 대상

추가 정보