When using the IBM Data Server Provider for .NET, a database
connection is established through the DB2Connection class.
About this task
To connect to a database:
Procedure
- 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.
- Pass the connectString to the DB2Connection constructor.
- Use the DB2Connection object's Open method
to formally connect to the database identified in connectString.