SqlParameterCollection.AddWithValue(String, Object) Metoda

Definice

Přidá hodnotu na konec .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

Parametry

parameterName
String

Název parametru

value
Object

Hodnota, která se má přidat. K označení hodnoty null použijte Value místo hodnoty null.

Návraty

Objekt SqlParameter .

Příklady

Následující příklad ukazuje, jak použít metodu 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

Poznámky

AddWithValuenahradí metoduSqlParameterCollection.Add, která přebírá a String .Object Přetížení objektu Add , který přebírá řetězec a objekt byl vyřazen z důvodu možné nejednoznačnosti s přetížením SqlParameterCollection.Add , které přebírá String a hodnotu výčtu SqlDbType , kde předání celého čísla s řetězcem může být interpretováno jako hodnota parametru nebo odpovídající SqlDbType hodnota. Použijte AddWithValue vždy, když chcete přidat parametr zadáním jeho názvu a hodnoty.

Pro SqlDbTypeXml hodnoty výčtu můžete použít řetězec, hodnotu XML, instanci odvozeného XmlReader typu nebo SqlXml objekt.

Platí pro

Viz také