tailieunhanh - Writing Database-Independent Code
[ Team LiB ] Recipe 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. | Team LiB Recipe 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. You want to create provider-independent code and use it with the provider-specific code that you might need. Solution The 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 be used 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 This event handler is provider-specific code that creates a SqlConnection and a SqlDataAdapter object and passes them as arguments into the providerindependent GetData method. OLE DB This event handler is provider-specific code that creates an OleDbConnection and an OleDbDataAdapter object and passes them as arguments into the providerindependent GetData method. The C code is shown in Example 1-9. Example 1-9. File Namespaces variables and constants using System using using using using using using . . . private void GetData IDbConnection conn IDbDataAdapter da Create the command and assign it to the IDbDataAdapter interface. IDbCommand cmd SELECT FROM Customers .
đang nạp các trang xem trước