Generic coding with the ADO.NET Core 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 OneDB™ .NET Data Provider are inherited from the System.Data.Common base classes. As a result, generic ADO.NET Core applications work with HCL OneDB databases through the HCL OneDB .NET Data provider.

The following C# code demonstrates a generic approach to establishing a database connection.
 DbProviderFactory factory = DbProviderFactories.GetFactory("Informix.Net.Core");
  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 Core application begins. This object creates generic instances of .NET Core provider objects, such as connections, data adapters, commands, and data readers, which work with a specific database product.

The "Informix.Net.Core" string that is passed into the GetFactory method uniquely identifies the HCL OneDB .NET Core Provider, and initializes a DbProviderFactory instance that creates database provider object instances specific to the HCL OneDB .NET Core Provider.

The DbConnection object can connect to HCL OneDB 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 HCL OneDB .NET Core Provider, and if so, generates a connection string to connect to the SAMPLE database.