Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

SqlParameterCollection::Item Property (Int32)

 

Gets the SqlParameter at the specified index.

Namespace:   System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)

public:
[BrowsableAttribute(false)]
property SqlParameter^ default[
	int index
] {
	SqlParameter^ get(int index);
	void set(int index, SqlParameter^ value);
}

Parameters

index
Type: System::Int32

The zero-based index of the parameter to retrieve.

Property Value

Type: System.Data.SqlClient::SqlParameter^

The SqlParameter at the specified index.

Exception Condition
IndexOutOfRangeException

The specified index does not exist.

The following example demonstrates creating SqlParameter objects to supply an input parameter to a stored procedure that returns results in an output parameter. The code iterates through the items in the SqlParameterCollection and displays some parameter properties in the console window. This example assumes a valid connection string to the AdventureWorks sample database on an instance of 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;
        }
    }
}

.NET Framework
Available since 1.1
Return to top
Show:
© 2017 Microsoft