SqlCommand.Parameters Propriété
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.
Obtient le SqlParameterCollection.
public:
property Microsoft::Data::SqlClient::SqlParameterCollection ^ Parameters { Microsoft::Data::SqlClient::SqlParameterCollection ^ get(); };
public Microsoft.Data.SqlClient.SqlParameterCollection Parameters { get; }
member this.Parameters : Microsoft.Data.SqlClient.SqlParameterCollection
Public ReadOnly Property Parameters As SqlParameterCollection
Valeur de propriété
Paramètres de l’instruction Transact-SQL ou de la procédure stockée. La valeur par défaut est une collection vide.
Exemples
L’exemple suivant montre comment créer et ajouter des SqlCommand paramètres au SqlParameterCollection.
using System;
using System.Data;
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = GetConnectionString();
string demo = @"<StoreSurvey xmlns=""http://schemas.microsoft.com/sqlserver/2004/07/adventure-works/StoreSurvey""><AnnualSales>1500000</AnnualSales><AnnualRevenue>150000</AnnualRevenue><BankName>Primary International</BankName><BusinessType>OS</BusinessType><YearOpened>1974</YearOpened><Specialty>Road</Specialty><SquareFeet>38000</SquareFeet><Brands>3</Brands><Internet>DSL</Internet><NumberEmployees>40</NumberEmployees></StoreSurvey>";
Int32 id = 3;
UpdateDemographics(id, demo, connectionString);
Console.ReadLine();
}
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);
}
}
}
static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Data Source=(local);Initial Catalog=AdventureWorks;"
+ "Integrated Security=SSPI";
}
}
Remarques
L’Microsoft .NET Framework Fournisseur de données pour SQL Server ne prend pas en charge l’espace réservé du point d’interrogation ( ?) pour passer des paramètres à une instruction SQL ou à une procédure stockée appelée par une commande de CommandType.Text. Dans ce cas, les paramètres nommés doivent être utilisés. Par exemple:
SELECT * FROM Customers WHERE CustomerID = @CustomerID
Note
Si les paramètres de la collection ne correspondent pas aux exigences de la requête à exécuter, une erreur peut se produire.
Pour plus d’informations, consultez Configuration des paramètres.