Writing Database-Independent Code
Số trang: 3
Loại file: pdf
Dung lượng: 14.10 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:
[ Team LiB ] Recipe 1.11 Writing Database-Independent Code Problem You need to develop an application that can be used with different data providers, but does not lose functionality that is specific to the different providers.
Nội dung trích xuất từ tài liệu:
Writing Database-Independent Code[ Team LiB ]Recipe 1.11 Writing Database-Independent CodeProblemYou need to develop an application that can be used with different data providers, butdoes not lose functionality that is specific to the different providers. You want to createprovider-independent code and use it with the provider-specific code that you mightneed.SolutionThe solution shows how to use interfaces that are inherited by .NET connected classes(such as Connection and DataReader) to create provider-independent code that can beused with provider-specific code to access unique functionality.The sample code contains a method and two event handlers:GetData( ) This method is a .NET data provider-independent method that accepts .NET data provider-specific Connection and DataAdapter arguments as generic IDbConnection and IDbDataAdapter interface types. The interfaces are used to fill a DataSet from the Customers table in Northwind. The default view of the Customers DataTable is bound to the data grid on the form. Finally, the provider-specific Connection for the IDbConnection is identified and provider-specific logic executed.SQL Button.Click This event handler is provider-specific code that creates a SqlConnection and a SqlDataAdapter object and passes them as arguments into the provider- independent GetData( ) method.OLE DB Button.Click This event handler is provider-specific code that creates an OleDbConnection and an OleDbDataAdapter object and passes them as arguments into the provider- independent GetData( ) method.The C# code is shown in Example 1-9.Example 1-9. File: DatabaseIndependentCodeForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Windows.Forms;using System.Data;using System.Data.Common;using System.Data.SqlClient;using System.Data.OleDb;// . . .private void GetData(IDbConnection conn, IDbDataAdapter da){ // Create the command and assign it to the IDbDataAdapter interface. IDbCommand cmd = conn.CreateCommand( ); cmd.CommandText = SELECT * FROM Customers; da.SelectCommand = cmd; // Add a table mapping. da.TableMappings.Add(Table, Customers); dataGrid.DataSource = null; // Fill the DataSet. DataSet ds = new DataSet( ); da.Fill(ds); // Bind the default view for the Customer table to the grid. dataGrid.DataSource = ds.Tables[Customers].DefaultView; // Identify provider-specific connection type and process appropriately. if (conn is SqlConnection) { MessageBox.Show(Specific processing for SQL data provider.); } else if(conn is OleDbConnection) { MessageBox.Show(Specific processing for OLE DB data provider.); }}private void sqlButton_Click(object sender, System.EventArgs e){ // Create a SQL Connection and DataAdapter. SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings[Sql_ConnectString]); SqlDataAdapter da = new SqlDataAdapter( ); dataGrid.CaptionText = SQL .NET Provider; // Call provider-independent function to retrieve data. GetData(conn, da);}private void oleDbButton_Click(object sender, System.EventArgs e){ // Create a OLE DB Connection and DataAdapter. OleDbConnection conn = new OleDbConnection( ConfigurationSettings.AppSettings[OleDb_ConnectString]); OleDbDataAdapter da = new OleDbDataAdapter( ); dataGrid.CaptionText = OLE DB .NET Provider; // Call provider-independent function to retrieve data. GetData(conn, da);}DiscussionThe IDbConnection, IDbCommand, IDataAdapter, and IDataReader interfaces areimplemented by Connection, Command, DataAdapter, and DataReader classes in .NETdata providers. You can pass these provider-independent base classes as interfacearguments instead of the provider-specific inherited classes. This allows applications thatsupport multiple data providers to reuse common provider-independent code.The provider-specific functionality of the classes is not available when the base interfacesare used. The is operator is used to identify the provider-specific class of the provider-independent interface. Branching logic is then used execute code specific to that class.[ Team LiB ]
Nội dung trích xuất từ tài liệu:
Writing Database-Independent Code[ Team LiB ]Recipe 1.11 Writing Database-Independent CodeProblemYou need to develop an application that can be used with different data providers, butdoes not lose functionality that is specific to the different providers. You want to createprovider-independent code and use it with the provider-specific code that you mightneed.SolutionThe solution shows how to use interfaces that are inherited by .NET connected classes(such as Connection and DataReader) to create provider-independent code that can beused with provider-specific code to access unique functionality.The sample code contains a method and two event handlers:GetData( ) This method is a .NET data provider-independent method that accepts .NET data provider-specific Connection and DataAdapter arguments as generic IDbConnection and IDbDataAdapter interface types. The interfaces are used to fill a DataSet from the Customers table in Northwind. The default view of the Customers DataTable is bound to the data grid on the form. Finally, the provider-specific Connection for the IDbConnection is identified and provider-specific logic executed.SQL Button.Click This event handler is provider-specific code that creates a SqlConnection and a SqlDataAdapter object and passes them as arguments into the provider- independent GetData( ) method.OLE DB Button.Click This event handler is provider-specific code that creates an OleDbConnection and an OleDbDataAdapter object and passes them as arguments into the provider- independent GetData( ) method.The C# code is shown in Example 1-9.Example 1-9. File: DatabaseIndependentCodeForm.cs// Namespaces, variables, and constantsusing System;using System.Configuration;using System.Windows.Forms;using System.Data;using System.Data.Common;using System.Data.SqlClient;using System.Data.OleDb;// . . .private void GetData(IDbConnection conn, IDbDataAdapter da){ // Create the command and assign it to the IDbDataAdapter interface. IDbCommand cmd = conn.CreateCommand( ); cmd.CommandText = SELECT * FROM Customers; da.SelectCommand = cmd; // Add a table mapping. da.TableMappings.Add(Table, Customers); dataGrid.DataSource = null; // Fill the DataSet. DataSet ds = new DataSet( ); da.Fill(ds); // Bind the default view for the Customer table to the grid. dataGrid.DataSource = ds.Tables[Customers].DefaultView; // Identify provider-specific connection type and process appropriately. if (conn is SqlConnection) { MessageBox.Show(Specific processing for SQL data provider.); } else if(conn is OleDbConnection) { MessageBox.Show(Specific processing for OLE DB data provider.); }}private void sqlButton_Click(object sender, System.EventArgs e){ // Create a SQL Connection and DataAdapter. SqlConnection conn = new SqlConnection( ConfigurationSettings.AppSettings[Sql_ConnectString]); SqlDataAdapter da = new SqlDataAdapter( ); dataGrid.CaptionText = SQL .NET Provider; // Call provider-independent function to retrieve data. GetData(conn, da);}private void oleDbButton_Click(object sender, System.EventArgs e){ // Create a OLE DB Connection and DataAdapter. OleDbConnection conn = new OleDbConnection( ConfigurationSettings.AppSettings[OleDb_ConnectString]); OleDbDataAdapter da = new OleDbDataAdapter( ); dataGrid.CaptionText = OLE DB .NET Provider; // Call provider-independent function to retrieve data. GetData(conn, da);}DiscussionThe IDbConnection, IDbCommand, IDataAdapter, and IDataReader interfaces areimplemented by Connection, Command, DataAdapter, and DataReader classes in .NETdata providers. You can pass these provider-independent base classes as interfacearguments instead of the provider-specific inherited classes. This allows applications thatsupport multiple data providers to reuse common provider-independent code.The provider-specific functionality of the classes is not available when the base interfacesare used. The is operator is used to identify the provider-specific class of the provider-independent interface. Branching logic is then used execute code specific to that class.[ 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 Writing Database-Independent CodeGợ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