Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Writing Database-Independent Code doc
Nội dung xem thử
Mô tả chi tiết
[ 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. 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 Button.Click
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 Button.Click
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.