SqlParameterCollection.Item[] 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會得到帶有指定屬性的 。SqlParameter
多載
| 名稱 | Description |
|---|---|
| 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); };
[System.ComponentModel.Browsable(false)]
public System.Data.SqlClient.SqlParameter this[string parameterName] { 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
參數
- 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); };
[System.ComponentModel.Browsable(false)]
public System.Data.SqlClient.SqlParameter this[int index] { 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
參數
- 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;
}
}
}