dotConnect for DB2 Documentation
In This Topic
    Logging Onto The Server
    In This Topic

    This tutorial describes how to connect to DB2 server.

    In this walkthrough:

    Requirements

    In order to connect to DB2 server you need a DB2 server itself running, dotConnect for DB2 and IBM DB2 .NET Data Provider installed and IDE running. You should know server name or IP address, the port, on which the server is listening, account login and password. Usually this information is enough.

    Note that if you do not use design-time (specifically, if you do not place the DB2Connection component from toolbox on a form designer), you have to embed licensing information manually. This is described in topic Licensing.

    General information

    To establish a connection to server you have to provide some connection parameters to dotConnect for DB2. This information is used by the DB2Connection component to find the server and login with credentials of your account. The parameters are represented as connection string. You can compose the connection string manually or have dotConnect for DB2 construct it for you.

    Creating DB2Connection

    Design time creation

    The following assumes that you have IDE running, and you are currently focused on a form designer.

    1. Open Toolbox and find DB2Connection component in dotConnect for DB2 category.
    2. Double-click the component. Note that new object appears on the designer underneath the form. If this is first time you create DB2Connection in this application, it is named db2Connection1.
    3. Click on the db2Connection1 object and press F4 to focus on object's properties.
    4. In the Server property provide DNS name or IP address of the computer where DB2 server resides and its port (separated by colon). For example, if DB2 server is running on same machine where you launch the application, this property can be set to localhost:50000 or 127.0.0.1:50000.
    5. In the UserId property specify your login. For example, root.
    6. In the Password property specify your password. For example, mypassword.
    7. In the Database property specify the database to connect to. For example, SAMPLE.
    8. Note that as you assign values to these properties the ConnectionString property is automatically updated to reflect your settings. Now it contains something like user id=db2admin;password=mypassword;server=db2:50000;database=SAMPLE.

    Run time creation

    Same operations performed in runtime look as follows (note that you have to add references to Devart.Data.DB2.dll and Devart.Data.dll assemblies):

    using Devart.Data.DB2;
    ...
    DB2Connection db2Connection1 = new DB2Connection();
    db2Connection1.Server = "db2:50000";
    db2Connection1.UserId = "db2admin";
    db2Connection1.Database = "SAMPLE";
    db2Connection1.Password = "mypassword";
    
    Imports Devart.Data.DB2
    ...
    Dim DB2Connection1 As DB2Connection = New DB2Connection()
    db2Connection1.Server = "db2:50000"
    db2Connection1.UserId = "db2admin"
    db2Connection1.Database = "SAMPLE"
    db2Connection1.Password = "mypassword"
    
    

    You can do this all in single assignment. It actually does not matter whether connection string is assigned directly or composed with particular properties. After you assign a value to ConnectionString property all other properties are populated with parsed values. So you can choose what is more convenient for you.

    db2Connection1.ConnectionString = "user id=db2admin;password=mypassword;server=db2:50000;database=SAMPLE";
    
    db2Connection1.ConnectionString = "user id=db2admin;password=mypassword;server=db2:50000;database=SAMPLE"
    
    

    Using connection string builder

    If you decide to setup a connection by assigning values to several properties, consider using the DB2ConnectionStringBuilder class. It has all of the possible connection settings exposed as properties, thus allowing you to customize the connection at full extent. The following example demonstrates how to compose a more complex connection string:

    DB2ConnectionStringBuilder myCSB = new DB2ConnectionStringBuilder();
    myCSB.Server = "db2:50000";
    myCSB.UserId = "db2admin";
    myCSB.Password = "mypassword";
    myCSB.Database = "SAMPLE";
    DB2Connection myConnection = new DB2Connection(myCSB.ConnectionString);
    
    Dim myCSB As DB2ConnectionStringBuilder = New DB2ConnectionStringBuilder
    myCSB.Server = "db2:50000"
    myCSB.UserId = "db2admin"
    myCSB.Password = "mypassword"
    myCSB.Database = "SAMPLE"
    Dim myConnection As DB2Connection = New DB2Connection(myCSB.ConnectionString)
    

    Note that in this example we used DB2Connection constructor that accepts connection string as argument.

    For the information on arguments allowed in the connection string, refer to the description of the DB2Connection.ConnectionString property.

    Opening connection

    Opening a connection is as simple as that:

    db2Connection1.Open();
    
    DB2Connection1.Open()
    
    

    Of course, DB2Connection1 must have a valid connection string assigned earlier. When you call Open, dotConnect for DB2 tries to find the host and connect to server. If any problem occurs it raises an exception with brief explanation on what is wrong. If no problem is encountered dotConnect for DB2 tries to establish the connection during ConnectionTimeout interval. Finally, when connection is established, the Open method returns and State property is changed to Open.

    In design time you can connect to server in few steps:

    1. Right-click on db2Connection1 object in form designer.
    2. Select Connect from the popup menu.
    3. In the dialog window provide necessary logon information.
    4. Click the Connect button to establish connection.

    Or you can simply change the State property to Open in the Properties window to establish a connection using the current connection string.

    Closing connection

    To close a connection call its Close method, or set its State property to Closed.

    The following example summarizes aforementioned information and shows how to create, setup, open, use and then close the connection.

    DB2Connection myConn = new DB2Connection();
    myConn.ConnectionString = "user id=db2admin;password=mypassword;server=db2:50000;database=SAMPLE";
    myConn.Open();
    MessageBox.Show(myConn.ServerVersion);
    myConn.Close();
    
    Dim myConn As DB2Connection = New DB2Connection()
    myConn.ConnectionString = "user id=db2admin;password=mypassword;server=db2:50000;database=SAMPLE"
    myConn.Open()
    MessageBox.Show(myConn.ServerVersion)
    myConn.Close()
    
    

    The sample code connects to a server, shows its version and then closes the connection. This actually is rare usage, because in real applications connections are used by other objects like DB2Command, DB2DataTable and others. For more information on this, please see the corresponding tutorials or the reference information.

    Modifying connection

    You can modify connection by changing properties of DB2Connection object. Keep in mind that while some of the properties can be altered freely, most of them close connection when new value is assigned. For example, if you change Database property, connection remains open, but if you change Server property, it gets closed immediately, and you have to reopen it manually.

    Additional information

    dotConnect for DB2 has wide set of features you can take advantage of. The following list enumerates some of them so you can explore the advanced techniques to achieve better performance or enable additional capabilities.

    See Also

    Getting Started  | DB2Connection Class