次の方法で共有


SqlCommand.ExecuteReader メソッド (CommandBehavior)

CommandTextConnection に送信し、 CommandBehavior 値のいずれかを使用して SqlDataReader を構築します。

Overloads Public Function ExecuteReader( _
   ByVal behavior As CommandBehavior _) As SqlDataReader
[C#]
public SqlDataReader ExecuteReader(CommandBehaviorbehavior);
[C++]
public: SqlDataReader* ExecuteReader(CommandBehaviorbehavior);
[JScript]
public function ExecuteReader(
   behavior : CommandBehavior) : SqlDataReader;

パラメータ

戻り値

SqlDataReader オブジェクト。

解説

CommandType プロパティを StoredProcedure に設定したときは、 CommandText プロパティをストアド プロシージャ名に設定する必要があります。 ExecuteReader を呼び出すと、このストアド プロシージャがコマンドによって実行されます。

SqlDataReader は、大きいバイナリ値を効率的に読み取るための特別なモードをサポートします。詳細については、 CommandBehaviorSequentialAccess 設定のトピックを参照してください。

SqlDataReader の使用中は、関連付けられた SqlConnection は、その SqlDataReader によって使用されるためビジー状態になります。この状態では、 SqlConnection に対して、閉じる以外の操作を実行できません。 SqlDataReader.Close メソッドを呼び出すまでは、この状態が続きます。 CommandBehaviorCloseConnection に設定して SqlDataReader を作成した場合、 SqlDataReader を閉じると、接続が自動的に閉じられます。

使用例

[Visual Basic, C#, C++] SqlCommand を作成し、Transact-SQL SELECT ステートメントの文字列とデータ ソースへの接続に使用する文字列を渡して実行する例を次に示します。 CommandBehaviorCloseConnection に設定します。

 
Public Sub CreateMySqlDataReader(mySelectQuery As String, _
myConnectionString As String)
    Dim myConnection As New SqlConnection(myConnectionString)
    Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
    myConnection.Open()
    Dim myReader As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    While myReader.Read()
        Console.WriteLine(myReader.GetString(0))
    End While
    myReader.Close()
    'Implicitly closes the connection because CommandBehavior.CloseConnection was specified.
 End Sub


[C#] 
public void CreateMySqlDataReader(string mySelectQuery,string myConnectionString) 
{
   SqlConnection myConnection = new SqlConnection(myConnectionString);
   SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);
   myConnection.Open();
   SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
   while(myReader.Read()) 
   {
      Console.WriteLine(myReader.GetString(0));
   }
   myReader.Close();
   //Implicitly closes the connection because CommandBehavior.CloseConnection was specified.
}


[C++] 
public:
    void CreateMySqlDataReader(String* mySelectQuery, String* myConnectionString)
    {
        SqlConnection* myConnection = new SqlConnection(myConnectionString);
        SqlCommand* myCommand = new SqlCommand(mySelectQuery, myConnection);
        myConnection->Open();
        SqlDataReader* myReader = myCommand->ExecuteReader(CommandBehavior::CloseConnection);
        while(myReader->Read())
        {
            Console::WriteLine(myReader->GetString(0));
        }
        myReader->Close();
        //Implicitly closes the connection because CommandBehavior::CloseConnection was specified.
    };

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

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

SqlCommand クラス | SqlCommand メンバ | System.Data.SqlClient 名前空間 | SqlCommand.ExecuteReader オーバーロードの一覧