Using ADO.NET Design-Time Features in Classes Without a GUI
Số trang: 4
Loại file: pdf
Dung lượng: 24.47 KB
Lượt xem: 8
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 7.17 Using ADO.NET Design-Time Features in Classes Without a GUI Problem The design-time environment provides controls and wizards to facilitate creation of and management of properties of ADO.NET objects.
Nội dung trích xuất từ tài liệu:
Using ADO.NET Design-Time Features in Classes Without a GUI[ Team LiB ]Recipe 7.17 Using ADO.NET Design-Time Features in Classes Without a GUIProblemThe design-time environment provides controls and wizards to facilitate creation of andmanagement of properties of ADO.NET objects. You want to use that design-timefunctionality when creating classes that do not have a GUI.SolutionCreate a component and use its design-time functionality.The solution contains two parts: the component and the test container for the component.To create the component Component0717.cs, add two controls to its design surface: • Drop a SqlDataAdapter onto the design surface and use the Data Adapter Configuration Wizard to connect to the Northwind sample database on the local SQL Server. Accept all wizard defaults; supply the following SQL statement when prompted to generate the SQL statements: • SELECT • OrderID, • CustomerID, • EmployeeID, • OrderDate, • RequiredDate, • ShippedDate, • ShipVia, • Freight, • ShipName, • ShipAddress, • ShipCity, • ShipRegion, • ShipPostalCode, • ShipCountry • FROM Orders After the wizard completes, rename the SqlDataAdapter control to da. • A SqlConnection control is automatically added to the design surface when the SqlDataAdapter wizard is completed; rename it to conn.The sample code for the component exposes one property and one method:MyDataTable A read-only property that returns a DataTable filled using the SqlDataAdapter control.Update( ) This method takes a DataTable object argument that uses the SqlDataAdapter control to update changes to the DataTable (retrieved using the MyDataTable property of the component) back to the database.The C# code for the component is shown in Example 7-33.Example 7-33. File: Component0717.cs// Namespaces, variables, and constantsusing System;using System.Data;using System.Data.SqlClient;// . . .public DataTable MyDataTable{ get { // Fill a table using the DataAdapter control. DataTable dt = new DataTable( ); da.Fill(dt); return dt; }}public void Update(DataTable dt){ // Update the table back to the data source. da.Update(dt);}The test container sample code contains two event handlers:Form.Load Instantiates the component Component0717 and binds the default view of the DataTable that it exposes (through the MyDataTable property) to the DataGrid on the form.Update Button.Click Instantiates the component Component0717 and calls the Update( ) method of the component to update changes made in the DataGrid to the DataTable retrieved in the Form.Load event handler.The C# code for the test container is shown in Example 7-34.Example 7-34. File: UsingDesignTimeFeauresWithComponentsForm.cs// Namespaces, variables, and constantsusing System;using System.Data;// . . .private void UsingDesignTimeFeauresWithComponentsForm_Load(object sender, System.EventArgs e){ // Bind the default of the table from the component to the grid. Component0717 c = new Component0717( ); dataGrid.DataSource = c.MyDataTable.DefaultView;}private void updateButton_Click(object sender, System.EventArgs e){ // Update the table to the data source using the component. Component0717 c = new Component0717( ); c.Update(((DataView)dataGrid.DataSource).Table);}DiscussionThe component and control are special-purpose classes in the .NET Framework: • A component is a class that implements the IComponent interface or inherits from a class that implements that interface, such as System.ComponentModel.Component. A component has no user interface; it supports a visual design surface similar to the component tray on controls with a visual design surface that you can use to add or arrange components used by the component. The arrangement of components on the design surface is not important because there is no user interface for a component. • A control is a component that provides user interface functionality and inherits from the System.Windows.Forms.Control class, which in turn derives from the Component. A control is a component, but a component is not necessarily a control.To create a component, simply right-click on a project or folder in the Solution Explorerwindow and select Add Add Component. The View Designer button in the SolutionExplorer window accesses the design surface of the component.[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Using ADO.NET Design-Time Features in Classes Without a GUI[ Team LiB ]Recipe 7.17 Using ADO.NET Design-Time Features in Classes Without a GUIProblemThe design-time environment provides controls and wizards to facilitate creation of andmanagement of properties of ADO.NET objects. You want to use that design-timefunctionality when creating classes that do not have a GUI.SolutionCreate a component and use its design-time functionality.The solution contains two parts: the component and the test container for the component.To create the component Component0717.cs, add two controls to its design surface: • Drop a SqlDataAdapter onto the design surface and use the Data Adapter Configuration Wizard to connect to the Northwind sample database on the local SQL Server. Accept all wizard defaults; supply the following SQL statement when prompted to generate the SQL statements: • SELECT • OrderID, • CustomerID, • EmployeeID, • OrderDate, • RequiredDate, • ShippedDate, • ShipVia, • Freight, • ShipName, • ShipAddress, • ShipCity, • ShipRegion, • ShipPostalCode, • ShipCountry • FROM Orders After the wizard completes, rename the SqlDataAdapter control to da. • A SqlConnection control is automatically added to the design surface when the SqlDataAdapter wizard is completed; rename it to conn.The sample code for the component exposes one property and one method:MyDataTable A read-only property that returns a DataTable filled using the SqlDataAdapter control.Update( ) This method takes a DataTable object argument that uses the SqlDataAdapter control to update changes to the DataTable (retrieved using the MyDataTable property of the component) back to the database.The C# code for the component is shown in Example 7-33.Example 7-33. File: Component0717.cs// Namespaces, variables, and constantsusing System;using System.Data;using System.Data.SqlClient;// . . .public DataTable MyDataTable{ get { // Fill a table using the DataAdapter control. DataTable dt = new DataTable( ); da.Fill(dt); return dt; }}public void Update(DataTable dt){ // Update the table back to the data source. da.Update(dt);}The test container sample code contains two event handlers:Form.Load Instantiates the component Component0717 and binds the default view of the DataTable that it exposes (through the MyDataTable property) to the DataGrid on the form.Update Button.Click Instantiates the component Component0717 and calls the Update( ) method of the component to update changes made in the DataGrid to the DataTable retrieved in the Form.Load event handler.The C# code for the test container is shown in Example 7-34.Example 7-34. File: UsingDesignTimeFeauresWithComponentsForm.cs// Namespaces, variables, and constantsusing System;using System.Data;// . . .private void UsingDesignTimeFeauresWithComponentsForm_Load(object sender, System.EventArgs e){ // Bind the default of the table from the component to the grid. Component0717 c = new Component0717( ); dataGrid.DataSource = c.MyDataTable.DefaultView;}private void updateButton_Click(object sender, System.EventArgs e){ // Update the table to the data source using the component. Component0717 c = new Component0717( ); c.Update(((DataView)dataGrid.DataSource).Table);}DiscussionThe component and control are special-purpose classes in the .NET Framework: • A component is a class that implements the IComponent interface or inherits from a class that implements that interface, such as System.ComponentModel.Component. A component has no user interface; it supports a visual design surface similar to the component tray on controls with a visual design surface that you can use to add or arrange components used by the component. The arrangement of components on the design surface is not important because there is no user interface for a component. • A control is a component that provides user interface functionality and inherits from the System.Windows.Forms.Control class, which in turn derives from the Component. A control is a component, but a component is not necessarily a control.To create a component, simply right-click on a project or folder in the Solution Explorerwindow and select Add Add Component. The View Designer button in the SolutionExplorer window accesses the design surface of the component.[ 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 Displaying an Image from a Database in a Web Forms ControlGợi ý tài liệu liên quan:
-
52 trang 417 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 301 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 287 0 0 -
74 trang 283 0 0
-
96 trang 283 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 270 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 268 1 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 260 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 252 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 251 0 0