次の方法で共有


SqlParameterCollection.IndexOf メソッド

SqlParameter の、コレクション内での位置を取得します。

オーバーロードの一覧

SqlParameter の、コレクション内での位置を取得します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Overridable Function IndexOf(Object) As Integer Implements IList.IndexOf

[C#] public virtual int IndexOf(object);

[C++] public: virtual int IndexOf(Object*);

[JScript] public function IndexOf(Object) : int;

指定したパラメータ名の SqlParameter の、コレクション内での位置を取得します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Overridable Function IndexOf(String) As Integer Implements IDataParameterCollection.IndexOf

[C#] public virtual int IndexOf(string);

[C++] public: virtual int IndexOf(String*);

[JScript] public function IndexOf(String) : int;

使用例

[Visual Basic, C#, C++] SqlParameterCollection コレクション内で、指定した ParameterNameSqlParameter を検索する例を次に示します。目的のパラメータが存在する場合は、そのパラメータの名前とインデックスを表示します。目的のパラメータが存在しない場合は、エラーを表示します。この例では、 SqlParameterCollection コレクションが SqlCommand によって既に作成されていることを前提にしています。

[Visual Basic, C#, C++] メモ   ここでは、IndexOf のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Public Sub SearchSqlParams()
    ' ...
    ' create SqlCommand myCmd
    ' ...
    If Not myCmd.Parameters.Contains("Description") Then
        MessageBox.Show("ERROR: no such parameter in the collection")
    Else
        MessageBox.Show(("match on parameter #" & myCmd.Parameters.IndexOf("Description").ToString()))
    End If
End Sub 'SearchSqlParams

[C#] 
public void SearchSqlParams() {
    // ...
    // create SqlCommand myCmd
    // ...
    if (!myCmd.Parameters.Contains("Description"))
       MessageBox.Show("ERROR: no such parameter in the collection");
    else
       MessageBox.Show("match on parameter #" +
                 myCmd.Parameters.IndexOf("Description").ToString());
 }

[C++] 
public:
void SearchSqlParams() {
    // ...
    // create SqlCommand myCmd
    // ...
    if (!myCmd->Parameters->Contains(S"Description"))
       MessageBox::Show(S"ERROR: no such parameter in the collection");
    else
       MessageBox::Show(String::Format( S"match on parameter #{0}", __box(myCmd->Parameters->IndexOf(S"Description"))));
 }

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

SqlParameterCollection クラス | SqlParameterCollection メンバ | System.Data.SqlClient 名前空間