SqlParameterCollection.AddWithValue(String, Object) Metodo

Definizione

Aggiunge un valore alla fine di 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

Parametri

parameterName
String

Nome del parametro.

value
Object

Valore da aggiungere. Utilizza Value anziché null, per indicare un valore null.

Restituisce

Un oggetto SqlParameter.

Esempio

Nell'esempio seguente viene illustrato l'utilizzo del metodo 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

Commenti

AddWithValue sostituisce il SqlParameterCollection.Add metodo che accetta un String oggetto e un Objectoggetto . L'overload di che accetta una stringa e un oggetto è stato deprecato a causa di possibili ambiguità con l'overload SqlParameterCollection.Add che accetta un String valore di enumerazione SqlDbType in cui il passaggio di Add un intero con la stringa può essere interpretato come il valore del parametro o il valore corrispondenteSqlDbType. Usare AddWithValue ogni volta che si vuole aggiungere un parametro specificando il nome e il valore.

Per SqlDbTypeXml i valori di enumerazione, è possibile usare una stringa, un valore XML, un'istanza XmlReader di tipo derivata o un SqlXml oggetto.

Si applica a

Vedi anche