tailieunhanh - Testing for No Records
[ Team LiB ] Recipe Testing for No Records Problem You need to determine whether any records were returned from a query that you just executed. Solution Use the property, the property, or the ( ) method. | Team LiB Recipe Testing for No Records Problem You need to determine whether any records were returned from a query that you just executed. Solution Use the property the property or the method. The sample code creates and fills a DataTable and uses the Count property of the DataRowCollection to determine if the query used to create the table returned any rows. Next a DataReader is created and both the HasRows property and the Read method are used to determine whether the query used to create the DataReader returned any rows. The C code is shown in Example 2-13. Example 2-13. File Namespaces variables and constants using System using using using using Table name constants private const String ORDERS_TABLE Orders . . . StringBuilder result new StringBuilder Fill the Orders DataTable. SqlDataAdapter da new SqlDataAdapter SELECT FROM Orders Sql_ConnectString DataTable orderTable new DataTable ORDERS_TABLE orderTable Test Orders DataTable for records. bool tableHasRecords 0 DataTable ORDERS_TABLE Has records tableHasRecords Create the Orders DataReader. SqlConnection conn new SqlConnection Sql_ConnectString SqlCommand cmd new SqlCommand SELECT FROM ORDERS conn SqlDataReader orderReader Test Orders DataReader for records. DataReader ORDERS_TABLE Has records Test Orders DataReader for records. bool readerHasRecords DataReader ORDERS_TABLE Has records readerHasRecords Discussion The DataTable contains a DataRowCollection object that contains all DataRow objects in the table. The DataRowCollection has
đang nạp các trang xem trước