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 は、 と を SqlParameterCollection.Add 受け取る String メソッドを Object置き換えます。 文字列と オブジェクトを受け取る のAddオーバーロードは、 と を受け取る列挙値をSqlParameterCollection.Add受け取るStringSqlDbTypeオーバーロードがあいまいである可能性があるため、非推奨となりました。この場合、文字列で整数を渡すと、パラメーター値または対応するSqlDbType値のいずれかとして解釈できます。 パラメーターの名前と値を指定して、パラメーターを追加する場合は常に を使用 AddWithValue します。

列挙値には SqlDbTypeXml 、文字列、XML 値、派生型インスタンス、 XmlReader または オブジェクトを SqlXml 使用できます。

適用対象

こちらもご覧ください