SqlParameterCollection.AddWithValue(String, Object) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Ajoute une valeur à la fin de l'objet 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
Paramètres
- parameterName
- String
Le nom du paramètre.
Retours
Objet SqlParameter.
Exemples
L'exemple suivant décrit comment utiliser la méthode 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
Remarques
AddWithValue remplace la SqlParameterCollection.Add
méthode qui prend un String et un Object. La surcharge de Add
qui prend une chaîne et un objet a été déprécié en raison d’une ambiguïté possible avec la SqlParameterCollection.Add
surcharge qui accepte un String et une valeur d’énumération SqlDbType où le passage d’un entier avec la chaîne peut être interprété comme étant la valeur de paramètre ou la valeur correspondante SqlDbType . Utilisez AddWithValue chaque fois que vous souhaitez ajouter un paramètre en spécifiant son nom et sa valeur.
Pour SqlDbTypeXml
les valeurs d’énumération, vous pouvez utiliser une chaîne, une valeur XML, un XmlReader type dérivé instance ou un SqlXml objet .
S’applique à
Voir aussi
Commentaires
https://aka.ms/ContentUserFeedback.
Bientôt disponible : Tout au long de 2024, nous allons supprimer progressivement GitHub Issues comme mécanisme de commentaires pour le contenu et le remplacer par un nouveau système de commentaires. Pour plus d’informations, consultezEnvoyer et afficher des commentaires pour