Pages

Tuesday, October 5, 2010

How to connect to 2 databases in Powerbuilder

How to connect to more than 1 database in Powerbuilder

Some times, you need to connect to more than 1 database in your application. In Powerbuilder, there's 1 object for database connection that you can create programmatically. The object call: TRANSACTION.

In this example, I will explain how to connect to 2 databases, which mean 1 is with default database connection object (named SQLCA), and the other is the object that we create mannualy.

I'm using Microsoft SQL Server 2000 as a database engine.

First of all, we need to create a transaction object. It's depending on what the purpose of the transaction. You can declare with Instance, Global or even Local variable for this object.

Let's say we create Instant Variable, called: SQLCAID

transaction SQLCAID;


On Open Event, we must create first the transaction object, then setup the properties of the object.

Note: You can try first on the Database painter, to get the right property values, and to make sure that you can connect to the database. Then copy and paste the script.

SQLCAID = CREATE transaction;
SQLCAID.DBMS = "MSS Microsoft SQL Server"
SQLCAID.Database = "database1"
SQLCAID.LogPass = "password"
SQLCAID.ServerName = "localhost"
SQLCAID.LogId = "sa"
SQLCAID.AutoCommit = False
SQLCAID.DBParm = ""


// Try to connect the database
// REMEMBER, you must add USING SQLCAID statement at the end of your SQL Statement, every times you want to execute your database that related with SQLCAID transaction

CONNECT USING SQLCAID;

// Trap the error if the object can't connect to database

IF SQLCAID.SQLCode <> 0 THEN
    MessageBox("Connection Error to "+SQLCAID.Database, &
        "Unable to connect to database. Make sure you type the correct password." + &
        "~r~nTry Again. If the problem persists, contact IT Officer" + &
        "~r~nSQLDBCode = " + String(SQLCAID.SQLDBCode) + &
        "~r~nSQLErrText = " + SQLCAID.SQLErrText)
   HALT
END IF



At the other side, you still must declare the DEFAULT SQLCA transaction object.

SQLCA.DBMS = "MSS Microsoft SQL Server"
SQLCA.Database = "databasedefault"
SQLCA.LogPass = "password"
SQLCA.ServerName = "localhost"
SQLCA.LogId = "sa"
SQLCA.AutoCommit = False
SQLCA.DBParm = ""


// Just for make sure that you connect with SQLCA transaction

CONNECT USING SQLCA;


// Trap the error if the object can't connect to database
IF SQLCA.SQLCode <> 0 THEN
    MessageBox("Connection Error to "+SQLCA.Database, &
        "Unable to connect to database. Make sure you type the correct password." + &
        "~r~nTry Again. If the problem persists, contact IT Officer" + &
        "~r~nSQLDBCode = " + String(SQLCA.SQLDBCode) + &
        "~r~nSQLErrText = " + SQLCA.SQLErrText)
   HALT
END IF


Now you already connected with 2 databases.

Remember, always put USING SQLCA or USING SQLCAID statement at the end of your SQL Statement to make sure that the SQL Statement will execute into the database that you purpose

Example:

string sProductID, sProductName

// will retrieve the product_id and product_name column from SQLCAID transaction
SELECT product_id, product_name INTO :sProductID, :sProductName FROM product_master USING SQLCAID;

// will retrieve the product_id and product_name column from SQLCA (Default) transaction
SELECT product_id, product_name INTO :sProductID, :sProductName FROM product_master USING SQLCA;

1 comment: