[ Team LiB ] |
Recipe 1.11 Writing Database-Independent CodeProblemYou 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. 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 be used with provider-specific code to access unique functionality. The sample code contains a method and two event handlers:
The C# code is shown in Example 1-9. Example 1-9. File: DatabaseIndependentCodeForm.cs// Namespaces, variables, and constants using 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 are implemented by Connection, Command, DataAdapter, and DataReader classes in .NET data providers. You can pass these provider-independent base classes as interface arguments instead of the provider-specific inherited classes. This allows applications that support multiple data providers to reuse common provider-independent code. The provider-specific functionality of the classes is not available when the base interfaces are 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 ] |