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

要添加的值。 使用 Value(而不是 null)指示空值。

返回

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 采用 StringObject的方法。 采用字符串和 对象的 的Add重载已被弃用,因为采用 和 SqlDbType 枚举值的重载String可能存在歧义性SqlParameterCollection.Add,其中传递包含字符串的整数可以解释为参数值或相应的SqlDbType值。 每当要通过指定参数的名称和值来添加参数时,请使用 AddWithValue

对于 SqlDbTypeXml 枚举值,可以使用字符串、XML 值、 XmlReader 派生类型实例或 SqlXml 对象。

适用于

另请参阅