Generic coding with the ADO.NET common base classes
The .NET Framework features a namespace that is called the System.Data.Common namespace, which contains a set of base classes that can be shared by any .NET data provider.
The main classes in the HCL Informix® .NET Data Provider are inherited from the System.Data.Common base classes. As a result, generic ADO.NET applications work with Informix® databases through the Informix® .NET Data provider.
DbProviderFactory factory = DbProviderFactories.GetFactory("IBM.Data.Informix");
DbConnection conn = factory.CreateConnection();
DbConnectionStringBuilder sb = factory.CreateConnectionStringBuilder();
if( sb.ContainsKey( "Database" ) )
{
sb.Remove( "database" );
sb.Add( "database", "SAMPLE" );
}
conn.ConnectionString = sb.ConnectionString;
conn.Open();
The DbProviderFactory
object is the point
where any generic ADO.NET application begins. This object creates
generic instances of .NET data provider objects, such as connections,
data adapters, commands, and data readers, which work with a specific
database product.
The "IBM.Data.Informix"
string
that is passed into the GetFactory
method uniquely
identifies the Informix® .NET
Data Provider, and initializes a DbProviderFactory
instance
that creates database provider object instances specific to the Informix® .NET
Data Provider.
The DbConnection
object can
connect to Informix® databases,
just as a IfxConnection
object, which is inherited
from the DbConnection
object.
By using the DbConnectionStringBuilder
class,
you can determine the connection string keywords for a data provider,
and generate a custom connection string. The code in the example checks
if a keyword named "database"
exists in the Informix® .NET
Data Provider, and if so, generates a connection string to connect
to the SAMPLE database.