Language

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を受け取るStringオーバーロードとSqlDbType列挙値とのあいまいさが考えられるため、非推奨となりました。この列挙値は、文字列で整数を渡すと、パラメーター値または対応するSqlDbType値のいずれかと解釈できます。 パラメーターの名前と値を指定してパラメーターを追加する場合は常に、 AddWithValue を使用します。

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

適用対象

こちらもご覧ください