DataTableReader.HasRows プロパティ
アセンブリ: System.Data (system.data.dll 内)



HasRows プロパティは、現在の結果セットに関する情報を返します。DataTableReader に結果セットが複数含まれている場合、NextResult メソッドを呼び出した直後に HasRows プロパティの値を調べ、新しい結果セットに行が含まれているかどうかを確認できます。
HasRows プロパティを使用することで、現在の結果セットに行が含まれていない場合に DataTableReader の Read メソッドを呼び出す必要がなくなります。

2 つの DataTable インスタンスにデータを格納する例を次に示します。1 番目の DataTable には行が 1 つ含まれ、2 番目には行が含まれていません。この例では 2 つの DataTable を含む DataTableReader を作成し、PrintData メソッドを呼び出してそれぞれの内容を表示します。PrintData を呼び出す前に、それぞれの HasRows プロパティの値を確認します。
Private Sub TestHasRows() 'Retrieve one row from the Store table: Dim customerTable As DataTable = GetCustomers() Dim productsTable As DataTable = GetProducts() Using reader As New DataTableReader( _ New DataTable() {customerTable, productsTable}) Do If reader.HasRows Then PrintData(reader) End If Loop While reader.NextResult() End Using Console.WriteLine("Press Enter to finish.") Console.ReadLine() End Sub Private Sub PrintData( _ ByVal reader As DataTableReader) ' Loop through all the rows in the DataTableReader. Do While reader.Read() For i As Integer = 0 To reader.FieldCount - 1 Console.Write("{0} ", reader(i)) Next Console.WriteLine() Loop End Sub Private Function GetCustomers() As DataTable ' Create sample Customers table, in order ' to demonstrate the behavior of the DataTableReader. Dim table As New DataTable ' Create two columns, ID and Name. Dim idColumn As DataColumn = table.Columns.Add("ID", GetType(Integer)) table.Columns.Add("Name", GetType(String)) ' Set the ID column as the primary key column. table.PrimaryKey = New DataColumn() {idColumn} table.Rows.Add(New Object() {1, "Mary"}) Return table End Function Private Function GetProducts() As DataTable ' Create sample Products table, in order ' to demonstrate the behavior of the DataTableReader. Dim table As New DataTable ' Create two columns, ID and Name. Dim idColumn As DataColumn = table.Columns.Add("ID", GetType(Integer)) table.Columns.Add("Name", GetType(String)) ' Set the ID column as the primary key column. table.PrimaryKey = New DataColumn() {idColumn} Return table End Function
private static void TestHasRows() { DataTable customerTable = GetCustomers(); DataTable productTable = GetProducts(); using (DataTableReader reader = new DataTableReader( new DataTable[] { customerTable, productTable })) { do { if (reader.HasRows) { PrintData(reader); } } while (reader.NextResult()); } Console.WriteLine("Press Enter to finish."); Console.ReadLine(); } private static void PrintData(DataTableReader reader) { // Loop through all the rows in the DataTableReader while (reader.Read()) { for (int i = 0; i < reader.FieldCount; i++) { Console.Write(reader[i] + " "); } Console.WriteLine(); } } private static DataTable GetCustomers() { // Create sample Customers table, in order // to demonstrate the behavior of the DataTableReader. DataTable table = new DataTable(); // Create two columns, ID and Name. DataColumn idColumn = table.Columns.Add("ID", typeof(int)); table.Columns.Add("Name", typeof(string )); // Set the ID column as the primary key column. table.PrimaryKey = new DataColumn[] { idColumn }; table.Rows.Add(new object[] { 1, "Mary" }); return table; } private static DataTable GetProducts() { // Create sample Products table, in order // to demonstrate the behavior of the DataTableReader. DataTable table = new DataTable(); // Create two columns, ID and Name. DataColumn idColumn = table.Columns.Add("ID", typeof(int)); table.Columns.Add("Name", typeof(string )); // Set the ID column as the primary key column. table.PrimaryKey = new DataColumn[] { idColumn }; return table; }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からDataTableReader.HasRows プロパティを検索する場合は、下記のリンクをクリックしてください。

- DataTableReader.HasRows プロパティのページへのリンク