SqlParameterCollection.Item[] Propriété

Définition

Obtient l'objet SqlParameter avec un attribut spécifié.

Surcharges

Item[String]

Obtient l'objet SqlParameter portant le nom spécifié.

Item[Int32]

Obtient l'objet SqlParameter à l'index spécifié.

Item[String]

Obtient l'objet SqlParameter portant le nom spécifié.

public:
 property System::Data::SqlClient::SqlParameter ^ default[System::String ^] { System::Data::SqlClient::SqlParameter ^ get(System::String ^ parameterName); void set(System::String ^ parameterName, System::Data::SqlClient::SqlParameter ^ value); };
public System.Data.SqlClient.SqlParameter this[string parameterName] { get; set; }
[System.ComponentModel.Browsable(false)]
public System.Data.SqlClient.SqlParameter this[string parameterName] { get; set; }
member this.Item(string) : System.Data.SqlClient.SqlParameter with get, set
[<System.ComponentModel.Browsable(false)>]
member this.Item(string) : System.Data.SqlClient.SqlParameter with get, set
Default Public Property Item(parameterName As String) As SqlParameter

Paramètres

parameterName
String

Nom du paramètre à récupérer.

Valeur de propriété

Objet SqlParameter portant le nom spécifié.

Attributs

Exceptions

La valeur parameterName spécifiée n'est pas valide.

Remarques

parameterName est utilisé pour rechercher la valeur d’index dans le sous-jacentSqlParameterCollection. Si n’est parameterName pas valide, un IndexOutOfRangeException est levée.

Voir aussi

S’applique à

Item[Int32]

Obtient l'objet SqlParameter à l'index spécifié.

public:
 property System::Data::SqlClient::SqlParameter ^ default[int] { System::Data::SqlClient::SqlParameter ^ get(int index); void set(int index, System::Data::SqlClient::SqlParameter ^ value); };
public System.Data.SqlClient.SqlParameter this[int index] { get; set; }
[System.ComponentModel.Browsable(false)]
public System.Data.SqlClient.SqlParameter this[int index] { get; set; }
member this.Item(int) : System.Data.SqlClient.SqlParameter with get, set
[<System.ComponentModel.Browsable(false)>]
member this.Item(int) : System.Data.SqlClient.SqlParameter with get, set
Default Public Property Item(index As Integer) As SqlParameter

Paramètres

index
Int32

Index de base zéro du paramètre à récupérer.

Valeur de propriété

SqlParameter au niveau de l'index spécifié.

Attributs

Exceptions

L'index spécifié n'existe pas.

Exemples

L’exemple suivant illustre la création d’objets SqlParameter pour fournir un paramètre d’entrée à une procédure stockée qui retourne des résultats dans un paramètre de sortie. Le code effectue une itération au sein des éléments du SqlParameterCollection et affiche certaines propriétés de paramètre dans la fenêtre de console. Cet exemple suppose une chaîne de connexion valide à l’exemple de base de données AdventureWorks sur une instance de SQL Server.

static private string CreateSqlParameters(int documentID)
{
    // Assumes GetConnectionString returns a valid connection string to the
    // AdventureWorks sample database on an instance of SQL Server 2005.
    using (SqlConnection connection =
               new SqlConnection(GetConnectionString()))
    {
        connection.Open();
        SqlCommand command = connection.CreateCommand();
        try
        {
            // Setup the command to execute the stored procedure.
            command.CommandText = "GetDocumentSummary";
            command.CommandType = CommandType.StoredProcedure;

            // Create the input parameter for the DocumentID.
            SqlParameter paramID =
                new SqlParameter("@DocumentID", SqlDbType.Int);
            paramID.Value = documentID;
            command.Parameters.Add(paramID);

            // Create the output parameter to retrieve the summary.
            SqlParameter paramSummary =
                new SqlParameter("@DocumentSummary", SqlDbType.NVarChar, -1);
            paramSummary.Direction = ParameterDirection.Output;
            command.Parameters.Add(paramSummary);

            // List the parameters and some of properties.
            SqlParameterCollection paramCollection = command.Parameters;
            string parameterList = "";
            for (int i = 0; i < paramCollection.Count; i++)
            {
                parameterList += String.Format("  {0}, {1}, {2}\n",
                    paramCollection[i], paramCollection[i].DbType,
                    paramCollection[i].Direction);
            }
            Console.WriteLine("Parameter Collection:\n" + parameterList);

            // Execute the stored procedure; retrieve
            // and display the output parameter value.
            command.ExecuteNonQuery();
            Console.WriteLine((String)(paramSummary.Value));
            return (String)(paramSummary.Value);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return null;
        }
    }
}

Voir aussi

S’applique à