SqlParameterCollection.AddWithValue(String, Object) 方法     
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将值添加到 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.SqlParameterPublic Function AddWithValue (parameterName As String, value As Object) As SqlParameter参数
- parameterName
- String
参数的名称。
返回
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 替换采用 String 和 Object的 SqlParameterCollection.Add 方法。 使用字符串和对象的 Add 重载被弃用,因为 SqlParameterCollection.Add 重载具有 String 和 SqlDbType 枚举值,其中传递包含字符串的整数可以解释为参数值或相应的 SqlDbType 值。 每当要通过指定参数名称和值来添加参数时,请使用 AddWithValue。
对于 SqlDbTypeXml 枚举值,可以使用字符串、XML 值、XmlReader 派生类型实例或 SqlXml 对象。
适用于
另请参阅
- 命令和参数 (ADO.NET)
- DataAdapter 参数 (ADO.NET)
- 使用适用于 SQL Server 的 .NET Framework 数据提供程序 
- ADO.NET 概述