Connecting to a database

You connect to a database by using the Open method of an IfxConnection object.

About this task

You define information about how to connect to the database (such as the machine and server where the database is located) by passing a connection string to the IfxConnection object. The connection string has the form:
attribute=value[;attribute=value]...

The brackets ([]) and the ellipsis (...) are not part of the string. They show that attribute/value pairs beyond the first are optional and any number of attribute/value pairs can be included. Separate attribute/value pairs from each other with a semicolon.

The full list of possible attributes is described in the topic IfxConnection class.

If you are using Microsoft™ Visual Studio you can create a connection visually:

Procedure

  1. Drag an IfxConnection from the Data tab of the toolbox onto one of your forms.
  2. Click in the ConnectionString property of the new IfxConnection object.
  3. Click the ellipses (...) button that appears in the ConnectionString text box.
    A dialog box opens.
  4. Complete the items of the dialog to provide the connection information.

Results

The following fragment shows a simple connection to a database called testdb on the HCL OneDB™ server that is called testserver that is located on a machine named berry:
IfxConnection conn=new IfxConnection("Host=berry; Service=9401; 
  Server=testserver;User ID=informix; password=ifxtest; 
  Database=testdb"); 
conn.Open();

An IfxConnection object can also determine the connection properties from the environment variables. If you set the properties in the connection string, the IfxConnection object uses those values. If you do not set the properties in the connection string, the IfxConnection object uses the values that are set by the environment.

However, you can set the value for the DELIMIDENT property in the registry by using the Setnet utility. If you do not set the value for the DELIMIDENT property in the connection string or in the environment, but you set it by using the Setnet utility, the IfxConnection object uses the value that you set with the Setnet utility.

In compliance with industry standards, the HCL® OneDB .NET Core Provider acts as though DELIMIDENT is set to Y unless you explicitly set it to N.

When your application finishes using the database, close the connection as in the following fragment:
conn.Close();

Connection string attribute names are not case-sensitive, but often their values are not.