SqlParameterCollection.AddWithValue(String, Object) 메서드

정의

SqlParameterCollection 끝에 값을 추가합니다.

public:
 System::Data::SqlClient::SqlParameter ^ AddWithValue(System::String ^ parameterName, System::Object ^ value);
public System.Data.SqlClient.SqlParameter AddWithValue (string parameterName, object value);
member this.AddWithValue : string * obj -> System.Data.SqlClient.SqlParameter
Public Function AddWithValue (parameterName As String, value As Object) As SqlParameter

매개 변수

parameterName
String

매개 변수의 이름입니다.

value
Object

추가할 값입니다. null 값을 나타내기 위해 null 대신 Value를 사용합니다.

반환

SqlParameter 개체입니다.

예제

다음 예제에서는 AddWithValue 메서드를 사용하는 방법을 보여 줍니다.

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

설명

AddWithValueSqlParameterCollection.Add 및 를 사용하는 메서드를 String 대체합니다 Object. 문자열과 개체를 사용하는 의 Add 오버로드는 를 사용하는 StringSqlDbType 오버로드와 SqlParameterCollection.Add 문자열을 사용하여 정수를 전달하는 열거형 값이 매개 변수 값 또는 해당 SqlDbType 값으로 해석될 수 있으므로 더 이상 사용되지 않습니다. 매개 변수의 이름과 값을 지정하여 매개 변수를 추가하려는 경우에 사용합니다 AddWithValue .

열거형 값의 경우 SqlDbTypeXml 문자열, XML 값, XmlReader 파생 형식 instance 또는 개체를 SqlXml 사용할 수 있습니다.

적용 대상

추가 정보