Compartir a través de


SqlParameterCollection.Item[] Propiedad

Definición

Sobrecargas

Item[String]

Obtiene el objeto SqlParameter con el nombre especificado.

Item[Int32]

Obtiene el objeto SqlParameter en el índice especificado.

Item[String]

Obtiene el objeto SqlParameter con el nombre especificado.

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

Parámetros

parameterName
String

Nombre del parámetro que se va a recuperar.

Valor de propiedad

Objeto SqlParameter con el nombre especificado.

Atributos

Excepciones

El valor especificado en parameterName no es válido.

Comentarios

parameterName se usa para buscar el valor de índice en el subyacenteSqlParameterCollection. Si no parameterName es válido, se producirá una IndexOutOfRangeException excepción .

Se aplica a

Item[Int32]

Obtiene el objeto SqlParameter en el índice especificado.

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

Parámetros

index
Int32

Índice de base cero del parámetro que se va a recuperar.

Valor de propiedad

Objeto SqlParameter en el índice especificado.

Atributos

Excepciones

El índice especificado no existe.

Ejemplos

En el ejemplo siguiente se muestra cómo crear SqlParameter objetos para proporcionar un parámetro de entrada a un procedimiento almacenado que devuelve resultados en un parámetro de salida. El código recorre en iteración los elementos de SqlParameterCollection y muestra algunas propiedades de parámetro en la ventana de la consola. En este ejemplo se supone una cadena de conexión válida a la base de datos de ejemplo AdventureWorks en una instancia de SQL Server.

using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
        // Supply any valid Document ID value.
        // The value 7 is supplied for demonstration purposes.
        string summaryString = CreateSqlParameters(7);
        Console.ReadLine();
    }
    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;
            }
        }
    }
    static private string GetConnectionString()
    {
        // To avoid storing the connection string in your code, 
        // you can retrieve it from a configuration file, using the 
        // System.Configuration.ConfigurationSettings.AppSettings property
        return "Data Source=(local);Initial Catalog=AdventureWorks;" +
            "Integrated Security=SSPI";
    }
}

Se aplica a