DataTableReader.HasRows 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,指出 DataTableReader 是否包含一個或多個資料列。
public:
virtual property bool HasRows { bool get(); };
public override bool HasRows { get; }
member this.HasRows : bool
Public Overrides ReadOnly Property HasRows As Boolean
屬性值
如果 DataTableReader 包含一個或更多資料列,則為 true
,否則為 false
。
例外狀況
嘗試擷取有關已關閉之 DataTableReader 的資訊。
範例
下列範例會將兩 DataTable 個實例填入數據。 第一個 DataTable 包含一個數據列,而第二個數據列則不包含任何數據列。 然後,此範例會建立包含這兩DataTable個DataTableReader物件的 ,並呼叫 PrintData 方法來顯示每個 物件的內容,並在呼叫 PrintData 之前檢查每個 屬性的值HasRows。
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;
}
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
備註
屬性 HasRows
會傳回目前結果集的相關信息。 DataTableReader如果 包含多個結果集,您可以在呼叫 NextResult 方法之後立即檢查 HasRows
屬性值,以判斷新的結果集是否包含數據列。
HasRows
使用 屬性,以避免在目前結果集中沒有數據列時呼叫 Read 的 DataTableReader 方法。