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
ConnectionandDataAdapterarguments as genericIDbConnectionandIDbDataAdapterinterface types. The interfaces are used to fill aDataSetfrom the Customers table in Northwind. The default view of the CustomersDataTableis bound to the data grid on the form.Finally, the provider-specific
Connectionfor theIDbConnectionis identified and provider-specific logic executed.- SQL
Button.Click This event handler is provider-specific code that creates a
SqlConnectionand aSqlDataAdapterobject and passes them as arguments into the provider-independentGetData( )method.- OLE DB
Button.Click This event handler is provider-specific code that creates an
OleDbConnectionand anOleDbDataAdapterobject and passes them as arguments into the provider-independentGetData( )method.
The ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access