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

설명

AddWithValue 는 a 및 .를 SqlParameterCollection.Add 사용하는 메서드를 StringObject바꿉니다. 문자열을 사용하는 오버로드는 Add 오버로드 StringSqlDbType 가 모호할 SqlParameterCollection.Add 수 있고 문자열을 사용하여 정수를 전달하는 열거형 값이 매개 변수 값 또는 해당 SqlDbType 값으로 해석될 수 있으므로 더 이상 사용되지 않습니다. 매개 변수의 이름과 값을 지정하여 매개 변수를 추가하려는 경우 언제든지 사용합니다 AddWithValue .

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

적용 대상

추가 정보