Pages

Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Monday, October 25, 2010

Unlock SA login in Microsoft SQL Server

If you or your team members who has access to your Microsoft SQL Server database with SA login, did type the wrong password three times, normally the SA login will be locked. It should be an error :

Login failed for 'sa' because the account is currently locked out. The system administrator can unlock it.

To solve this problem, you need an authority to access the database server directly. Login with the administrator login name, then open Microsoft SQL Server Management Studio. Choose Windows Authentication mode, then click Connect.



Now, in Query windows type this statement then execute

ALTER LOGIN sa WITH PASSWORD = 'sa_password' UNLOCK


There's an option to avoid the sa login to be locked forever, just go to Security, Logins, sa, Properties, then uncheck Enforce password policy option.
»»  READMORE...

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;
»»  READMORE...

Thursday, July 22, 2010

What is Embedded SQL

Note: I'm the author of this article. I published 4 articles at http://allaboutpowerbuilder.blogspot.com a year ago, and some how I lost the log-in ID for the account :( So, I re-write the article in my new blog.

Embedded SQL is one of main feature in Powerbuilder. It's make possible to programmer to write the SQL Statement directly without declare in a variable.

Unlike the other programming language, In Powerbuilder you can write the SQL statement in script painter. It make more faster and more efficient for programmer, because the Powerbuilder compiler execute directly the statement and tell the programmer if they have a wrong SQL syntax.

The features supported when you use embedded SQL depend on the DBMS to which your application connects.

Since I've experiences in Ms. SQL Server for the database, I will show you an example with Ms. SQL Server SQL Statement. When you use the SQL Server database interface, you can embed the following types of SQL statements in scripts and user-defined functions:
  • Transaction management statements
  • Non-cursor statements
  • Cursor statements
  • Database stored procedures
See the picture for the example:


»»  READMORE...