DB2 Version 10.1 for Linux, UNIX, and Windows

Connecting to a database from an application using the IBM Data Server Provider for .NET

When using the IBM® Data Server Provider for .NET, a database connection is established through the DB2Connection class.

Procedure

To connect to a database:

  1. Create a string that stores the connection parameters. The format for a typical connection string format is:
    Server=<ip address/localhost>:<port number>; 
    Database=<db name>;
    UID=<userID>; 
    PWD=<password>; 
    Connect Timeout=<Timeout value>
    Examples of possible connection strings are:
    Example 1:
    String connectString = "Database=SAMPLE";
    // When used, attempts to connect to the SAMPLE database. 
    Note: If you specify only the database name in the connection string, the other information such as the server, userid, and password, must be included in the db2dsdriver.cfg file.
    Example 2:
    String cs = "Server=srv:50000;
    Database=SAMPLE;
    UID=db2adm;
    PWD=ab1d;Connect Timeout=30";
    // When used, attempts to connect to the SAMPLE database on the server
    // 'srv' through port 50000 using 'db2adm' and 'ab1d' as the user id and
    // password respectively. If the connection attempt takes
    // more than thirty seconds, the attempt will be terminated and an error
    // will be generated.
  2. Pass the connectString to the DB2Connection constructor.
    • Connecting to a database in C#:
      String connectString = "Database=SAMPLE";
      DB2Connection conn = new DB2Connection(connectString);
      conn.Open();
      return conn;
    • Connecting to a database in Visual Basic .NET:
      Dim connectString As String = "Database=SAMPLE"
      Dim conn As DB2Connection = new DB2Connection(connectString)
      conn.Open()
      Return conn
  3. Use the DB2Connection object's Open method to formally connect to the database identified in connectString.