SqlParameterCollection.Item[] 属性   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取具有指定属性的 SqlParameter。
重载
| Item[String] | 获取具有指定名称的 SqlParameter。 | 
| Item[Int32] | 获取位于指定索引处的 SqlParameter。 | 
Item[String]
获取具有指定名称的 SqlParameter。
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, setDefault Public Property Item(parameterName As String) As SqlParameter参数
- parameterName
- String
要检索的参数的名称。
属性值
具有指定名称的 SqlParameter。
- 属性
例外
指定的 parameterName 无效。
注解
              parameterName用于查找基础 SqlParameterCollection中的索引值。 
              parameterName如果 无效,IndexOutOfRangeException将引发 。
另请参阅
适用于
Item[Int32]
获取位于指定索引处的 SqlParameter。
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, setDefault Public Property Item(index As Integer) As SqlParameter参数
- index
- Int32
要检索的参数的索引(从零开始)。
属性值
指定索引处的 SqlParameter。
- 属性
例外
指定的索引不存在。
示例
以下示例演示如何创建 SqlParameter 对象,以便向返回输出参数结果的存储过程提供输入参数。 代码循环访问 中的 SqlParameterCollection 项,并在控制台窗口中显示一些参数属性。 此示例假定对 SQL Server 实例上的 AdventureWorks 示例数据库的有效连接字符串。
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;
        }
    }
}