Refreshing a DataSet Automatically Using Extended Properties
Số trang: 4
Loại file: pdf
Dung lượng: 25.96 KB
Lượt xem: 2
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:
Refreshing a DataSet Automatically Using Extended Properties Problem You need to automatically refresh a DataSet periodically. Solution Use extended properties and a timer. The sample code contains two event handlers and one method:
Nội dung trích xuất từ tài liệu:
Refreshing a DataSet Automatically Using Extended Properties[ Team LiB ]Recipe 9.14 Refreshing a DataSet Automatically Using Extended PropertiesProblemYou need to automatically refresh a DataSet periodically.SolutionUse extended properties and a timer.The sample code contains two event handlers and one method:Form.Load Sets up the sample by creating a DataTable containing the Categories table from the Northwind database. The default view of the table is bound to a data grid on the form. A second DataTable with the auto-refreshing functionality is created that also contains the Categories table from the Northwind database. The default view of the auto-refreshing table is bound to a second data grid on the form. An extended property RefreshTime is added to the auto-refreshing table and set to 15 seconds—the value of the constant DATAREFRESH_SECONDS in the sample— into the future. Finally, a thread timer is created with a TimerClassback delegate CheckRefreshDataSet, with a due time of one second, and a period of one second.Update Button.Click Uses a DataAdapter to update changes made to the first DataTable back to the data source.CheckRefreshDataSet( ) This method is called periodically by the thread timer. The method checks whether the current time is later than the time in the RefreshTime extended property of the auto-refreshing table. If it is, a DataAdapter is used to fill the table with the latest data from the data source and the RefreshTime extended property is once again set to 15 seconds into the future.The C# code is shown in Example 9-17.Example 9-17. File: AutomaticRefreshDataSetForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Threading;using System.Data;using System.Data.SqlClient;private const String CATEGORIES_TABLE = Categories;private const int DATAREFRESH_SECONDS = 15;private const int DATASETCHECKREFRESHINTERVAL_MS = 1000;private DataTable dt, dtRefresh;private SqlDataAdapter da, daRefresh;private System.Threading.Timer timer;// ..private void AutomaticRefreshDataSetForm_Load(object sender, System.EventArgs e){ String sqlText = SELECT CategoryID, CategoryName, Description FROM Categories; // Fill the categories table for editing. da = new SqlDataAdapter(sqlText, ConfigurationSettings.AppSettings[Sql_ConnectString]); SqlCommandBuilder cd = new SqlCommandBuilder(da); dt = new DataTable(CATEGORIES_TABLE); da.FillSchema(dt, SchemaType.Source); da.Fill(dt); // Bind the default view of the table to the grid. dataGrid.DataSource = dt.DefaultView; // Fill the autorefresh categories table. daRefresh = new SqlDataAdapter(sqlText, ConfigurationSettings.AppSettings[Sql_ConnectString]); dtRefresh = new DataTable(CATEGORIES_TABLE); daRefresh.FillSchema(dtRefresh, SchemaType.Source); daRefresh.Fill(dtRefresh); // Bind the default view of the table to the grid. refreshDataGrid.DataSource = dtRefresh.DefaultView; // Set the refresh time for the data. dtRefresh.ExtendedProperties[RefreshTime] = DateTime.Now.AddSeconds(DATAREFRESH_SECONDS).ToString( ); // Start the timer. timer = new System.Threading.Timer( new TimerCallback(CheckRefreshDatabase), null, DATASETCHECKREFRESHINTERVAL_MS,DATASETCHECKREFRESHINTERVAL_MS);}private void updateButton_Click(object sender, System.EventArgs e){ // Update the categories edited to the data source. da.Update(dt);}private void CheckRefreshDatabase(Object state){ DateTime now = DateTime.Now; // Check if the specified number of seconds have elapsed. if (Convert.ToDateTime(dtRefresh.ExtendedProperties [RefreshTime].ToString( )) < now) { // Refresh the table. daRefresh.Fill(dtRefresh); // Update the next refresh time. dtRefresh.ExtendedProperties[RefreshTime] = now.AddSeconds(DATAREFRESH_SECONDS).ToString( ); resultTextBox.Text = Table refreshed ( + now.ToString(T) + ) + Environment.NewLine + resultTextBox.Text; }}DiscussionThe ExtendedProperties property accesses a PropertyCollection of custom informationfor a DataSet, DataTable, DataColumn, DataRelation, or Constraint object. ThePropertyCollection extends the Hashtable class to store information as a collection ofkey-and-value pairs. The extended property data must be stored as strings; otherwise, itwill not be persisted when the data is written as XML. Add items to the collection usingthe Add( ) method, remove them with the Remove( ) method, and access them using theindexer in C# or the Item( ) property in VB.NET. For more information about membersof the PropertyCollection class, see the MSDN Library.There are three timers in the Visual Studio .NET and the .NET Framework: • The Windows-based timer System.Windows.Form.Timer (available on the Windows Form tab of the Toolbox) is designed for a single-threaded environment where UI threads are used for processing. This is the simplest timer to use but also the least accurate with an accuracy limited to 55ms. • The thread timer System.Threading.Timer is a simple, lightweight timer that uses callback methods to periodically run a task on a separate thread. This timer can only be used programmatically. This timer is more accurate than the Windows- based timer since the time interval between callbacks is specified in milliseconds. • The server-based timer System.Timers.Timer (available on the Components tab of the Toolbox) is designed for use with worker threads in a multi-threaded environment. This timer uses server ticks generated out-of-process and is the most accurate of the three since the time interval between timer event firings ...
Nội dung trích xuất từ tài liệu:
Refreshing a DataSet Automatically Using Extended Properties[ Team LiB ]Recipe 9.14 Refreshing a DataSet Automatically Using Extended PropertiesProblemYou need to automatically refresh a DataSet periodically.SolutionUse extended properties and a timer.The sample code contains two event handlers and one method:Form.Load Sets up the sample by creating a DataTable containing the Categories table from the Northwind database. The default view of the table is bound to a data grid on the form. A second DataTable with the auto-refreshing functionality is created that also contains the Categories table from the Northwind database. The default view of the auto-refreshing table is bound to a second data grid on the form. An extended property RefreshTime is added to the auto-refreshing table and set to 15 seconds—the value of the constant DATAREFRESH_SECONDS in the sample— into the future. Finally, a thread timer is created with a TimerClassback delegate CheckRefreshDataSet, with a due time of one second, and a period of one second.Update Button.Click Uses a DataAdapter to update changes made to the first DataTable back to the data source.CheckRefreshDataSet( ) This method is called periodically by the thread timer. The method checks whether the current time is later than the time in the RefreshTime extended property of the auto-refreshing table. If it is, a DataAdapter is used to fill the table with the latest data from the data source and the RefreshTime extended property is once again set to 15 seconds into the future.The C# code is shown in Example 9-17.Example 9-17. File: AutomaticRefreshDataSetForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Threading;using System.Data;using System.Data.SqlClient;private const String CATEGORIES_TABLE = Categories;private const int DATAREFRESH_SECONDS = 15;private const int DATASETCHECKREFRESHINTERVAL_MS = 1000;private DataTable dt, dtRefresh;private SqlDataAdapter da, daRefresh;private System.Threading.Timer timer;// ..private void AutomaticRefreshDataSetForm_Load(object sender, System.EventArgs e){ String sqlText = SELECT CategoryID, CategoryName, Description FROM Categories; // Fill the categories table for editing. da = new SqlDataAdapter(sqlText, ConfigurationSettings.AppSettings[Sql_ConnectString]); SqlCommandBuilder cd = new SqlCommandBuilder(da); dt = new DataTable(CATEGORIES_TABLE); da.FillSchema(dt, SchemaType.Source); da.Fill(dt); // Bind the default view of the table to the grid. dataGrid.DataSource = dt.DefaultView; // Fill the autorefresh categories table. daRefresh = new SqlDataAdapter(sqlText, ConfigurationSettings.AppSettings[Sql_ConnectString]); dtRefresh = new DataTable(CATEGORIES_TABLE); daRefresh.FillSchema(dtRefresh, SchemaType.Source); daRefresh.Fill(dtRefresh); // Bind the default view of the table to the grid. refreshDataGrid.DataSource = dtRefresh.DefaultView; // Set the refresh time for the data. dtRefresh.ExtendedProperties[RefreshTime] = DateTime.Now.AddSeconds(DATAREFRESH_SECONDS).ToString( ); // Start the timer. timer = new System.Threading.Timer( new TimerCallback(CheckRefreshDatabase), null, DATASETCHECKREFRESHINTERVAL_MS,DATASETCHECKREFRESHINTERVAL_MS);}private void updateButton_Click(object sender, System.EventArgs e){ // Update the categories edited to the data source. da.Update(dt);}private void CheckRefreshDatabase(Object state){ DateTime now = DateTime.Now; // Check if the specified number of seconds have elapsed. if (Convert.ToDateTime(dtRefresh.ExtendedProperties [RefreshTime].ToString( )) < now) { // Refresh the table. daRefresh.Fill(dtRefresh); // Update the next refresh time. dtRefresh.ExtendedProperties[RefreshTime] = now.AddSeconds(DATAREFRESH_SECONDS).ToString( ); resultTextBox.Text = Table refreshed ( + now.ToString(T) + ) + Environment.NewLine + resultTextBox.Text; }}DiscussionThe ExtendedProperties property accesses a PropertyCollection of custom informationfor a DataSet, DataTable, DataColumn, DataRelation, or Constraint object. ThePropertyCollection extends the Hashtable class to store information as a collection ofkey-and-value pairs. The extended property data must be stored as strings; otherwise, itwill not be persisted when the data is written as XML. Add items to the collection usingthe Add( ) method, remove them with the Remove( ) method, and access them using theindexer in C# or the Item( ) property in VB.NET. For more information about membersof the PropertyCollection class, see the MSDN Library.There are three timers in the Visual Studio .NET and the .NET Framework: • The Windows-based timer System.Windows.Form.Timer (available on the Windows Form tab of the Toolbox) is designed for a single-threaded environment where UI threads are used for processing. This is the simplest timer to use but also the least accurate with an accuracy limited to 55ms. • The thread timer System.Threading.Timer is a simple, lightweight timer that uses callback methods to periodically run a task on a separate thread. This timer can only be used programmatically. This timer is more accurate than the Windows- based timer since the time interval between callbacks is specified in milliseconds. • The server-based timer System.Timers.Timer (available on the Components tab of the Toolbox) is designed for use with worker threads in a multi-threaded environment. This timer uses server ticks generated out-of-process and is the most accurate of the three since the time interval between timer event firings ...
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 Refreshing a DataSet Automatically Using Extended PropertiesTài liệu có liên quan:
-
52 trang 469 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 369 0 0 -
96 trang 335 0 0
-
74 trang 329 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 322 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 321 1 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 311 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 305 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 304 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 297 0 0