SqlParameterCollection.AddWithValue(String, Object) Método

Definición

Agrega un valor al final de .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

Parámetros

parameterName
String

Nombre del parámetro.

value
Object

Valor que se va a agregar. Use Value en lugar de null para indicar un valor NULL.

Devoluciones

Un objeto SqlParameter.

Ejemplos

En el ejemplo siguiente se muestra cómo usar el AddWithValue método .

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

Comentarios

AddWithValuereemplaza el SqlParameterCollection.Add método que toma y String .Object La sobrecarga de Add que toma una cadena y un objeto está en desuso debido a la posible ambigüedad con la SqlParameterCollection.Add sobrecarga que toma un String valor de enumeración y donde SqlDbType pasar un entero con la cadena podría interpretarse como el valor del parámetro o el valor correspondiente SqlDbType . Use AddWithValue siempre que quiera agregar un parámetro especificando su nombre y valor.

Para SqlDbTypeXml los valores de enumeración, puede usar una cadena, un valor XML, una XmlReader instancia de tipo derivado o un SqlXml objeto .

Se aplica a

Consulte también