Ability to connect to the database with ADO.NET

[Note]
  • [INFOCENTER_V9] Connecting to a database from an application using the DB2 .NET Data Provider

  • [INFOCENTER_V9] DB2Connection Class

Connecting to a database from an application using the DB2 .NET Data Provider

When using the DB2 .NET Data Provider, a database connection is established through the DB2Connection class. First, you must create a string that stores the connection parameters.

Examples of possible connection strings are:

String connectString = "Database=SAMPLE";
// When used, attempts to connect to the SAMPLE database.					
					
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.
					

To create the database connection, pass the connectString to the DB2Connection constructor. Then use the DB2Connection object's Open method to formally connect to the database identified in connectString.

Connecting to a database in C#:

String connectString = "Database=SAMPLE";
DB2Connection conn = new DB2Connection(connectString);
conn.Open();
return conn;
					

DB2Connection

DB2Connection class represents an open connection to a database.

A DB2Connection object represents a unique connection to the database specfied in the connection string. In the case of a client/server database system, it is equivalent to a network connection to the server.

The DB2Connection object uses native resources. You should always explicitly close any open DB2Connection objects by calling Close or Dispose before the DB2Connection object goes out of scope. Not doing so leaves the freeing of these native resources to garbage collection, which may not free them immediately. This, in turn, may eventually cause the IBM Data Server Provider for .NET or data server to run out of resources or reach a maximum limit, resulting in sporadic failures. For example, you might encounter Maximum Connections-related errors while a number of connections are waiting to be deleted by the garbage collector. Explicitly closing the connections by calling Close or Dispose allows a more efficient use of native resources, enhancing scalability and improving overall application performance.

The following C# example creates a DB2Command and an DB2Connection. The DB2Connection is opened and set as the DB2Command.Connection property. The example then calls DB2Command.ExecuteNonQuery, and closes the connection. To accomplish this, the ExecuteNonQuery is passed a connection string and a query string that is an SQL INSERT statement:

public void InsertRow(string myConnectionString)
{
   // If the connection string is null, use a default.
   if(myConnectionString == "")
   {
      myConnectionString = "DATABASE=SAMPLE;";
   }
   DB2Connection myConn = new DB2Connection(myConnectionString);
   string myInsertQuery = "INSERT INTO STAFF (ID, NAME) Values(...)";
   DB2Command myDB2Command = new DB2Command(myInsertQuery);
   myDB2Command.Connection = myConn;
   myConn.Open();
   myDB2Command.ExecuteNonQuery();
   myConn.Close();
}					
					

Note: The SAMPLE database must be cataloged before a user or application can connect to it.

Professional hosting     Belorussian informational portal         Free SCWCD 1.4 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide     SCDJWS 5.0 Quiz