Testing for No Records
Số trang: 2
Loại file: pdf
Dung lượng: 12.48 KB
Lượt xem: 4
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
[ Team LiB ] Recipe 2.11 Testing for No Records Problem You need to determine whether any records were returned from a query that you just executed. Solution Use the DataRowCollection.Count property, the DataReader.HasRows property, or the DataReader.Read( ) method.
Nội dung trích xuất từ tài liệu:
Testing for No Records [ Team LiB ]Recipe 2.11 Testing for No RecordsProblemYou need to determine whether any records were returned from a query that you justexecuted.SolutionUse the DataRowCollection.Count property, the DataReader.HasRows property, or theDataReader.Read( ) method.The sample code creates and fills a DataTable and uses the Count property of theDataRowCollection 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 areused 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: NoRecordTestForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Text;using System.Data;using System.Data.SqlClient;// Table name constantsprivate const String ORDERS_TABLE = Orders;// . . .StringBuilder result = new StringBuilder( );// Fill the Orders DataTable.SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM Orders, ConfigurationSettings.AppSettings[Sql_ConnectString]);DataTable orderTable = new DataTable(ORDERS_TABLE);da.Fill(orderTable);// Test Orders DataTable for records.bool tableHasRecords = orderTable.Rows.Count > 0;result.Append(DataTable + ORDERS_TABLE + : Has records = + tableHasRecords + Environment.NewLine);// Create the Orders DataReader.SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings[Sql_ConnectString]);SqlCommand cmd = new SqlCommand(SELECT * FROM ORDERS, conn);conn.Open( );SqlDataReader orderReader = cmd.ExecuteReader( );// Test Orders DataReader for records.result.Append(DataReader + ORDERS_TABLE + : Has records = + orderReader.HasRows + Environment.NewLine);// Test Orders DataReader for records.bool readerHasRecords = orderReader.Read( );result.Append(DataReader + ORDERS_TABLE + : Has records = + readerHasRecords + Environment.NewLine);orderReader.Close( );conn.Close( );resultTextBox.Text = result.ToString( );DiscussionThe DataTable contains a DataRowCollection object that contains all DataRow objects inthe table. The DataRowCollection has a Count property that returns the number of rowsin the table. The Count property for an empty table has a value of 0.The HasRows property of the DataReader returns a Boolean value indicating whether theDataReader has any records.Another way is to use the Read( ) method to advance the DataReader to the next record.This returns a value of true if a record is available and false otherwise. The first call tothe Read( ) method will indicate whether any records were returned by the DataReader.This was the only way to determine whether the DataReader contained any records priorto the introduction of the HasRows property in .NET Framework 1.1.[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Testing for No Records [ Team LiB ]Recipe 2.11 Testing for No RecordsProblemYou need to determine whether any records were returned from a query that you justexecuted.SolutionUse the DataRowCollection.Count property, the DataReader.HasRows property, or theDataReader.Read( ) method.The sample code creates and fills a DataTable and uses the Count property of theDataRowCollection 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 areused 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: NoRecordTestForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Text;using System.Data;using System.Data.SqlClient;// Table name constantsprivate const String ORDERS_TABLE = Orders;// . . .StringBuilder result = new StringBuilder( );// Fill the Orders DataTable.SqlDataAdapter da = new SqlDataAdapter(SELECT * FROM Orders, ConfigurationSettings.AppSettings[Sql_ConnectString]);DataTable orderTable = new DataTable(ORDERS_TABLE);da.Fill(orderTable);// Test Orders DataTable for records.bool tableHasRecords = orderTable.Rows.Count > 0;result.Append(DataTable + ORDERS_TABLE + : Has records = + tableHasRecords + Environment.NewLine);// Create the Orders DataReader.SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings[Sql_ConnectString]);SqlCommand cmd = new SqlCommand(SELECT * FROM ORDERS, conn);conn.Open( );SqlDataReader orderReader = cmd.ExecuteReader( );// Test Orders DataReader for records.result.Append(DataReader + ORDERS_TABLE + : Has records = + orderReader.HasRows + Environment.NewLine);// Test Orders DataReader for records.bool readerHasRecords = orderReader.Read( );result.Append(DataReader + ORDERS_TABLE + : Has records = + readerHasRecords + Environment.NewLine);orderReader.Close( );conn.Close( );resultTextBox.Text = result.ToString( );DiscussionThe DataTable contains a DataRowCollection object that contains all DataRow objects inthe table. The DataRowCollection has a Count property that returns the number of rowsin the table. The Count property for an empty table has a value of 0.The HasRows property of the DataReader returns a Boolean value indicating whether theDataReader has any records.Another way is to use the Read( ) method to advance the DataReader to the next record.This returns a value of true if a record is available and false otherwise. The first call tothe Read( ) method will indicate whether any records were returned by the DataReader.This was the only way to determine whether the DataReader contained any records priorto the introduction of the HasRows property in .NET Framework 1.1.[ Team LiB ]
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Testing for No RecordsGợi ý tài liệu liên quan:
-
52 trang 430 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 314 0 0 -
74 trang 299 0 0
-
96 trang 293 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 289 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 281 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 275 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 265 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 265 0 0