Chapter 3 DynaScript Predefined Objects


connection methods

The connection object has these methods:

Commit method

Syntax

connection.Commit (  )

Description

Commits the transaction.

Return

Boolean.

Example

This example moves 100 dollars from a checking account to a savings account. If the money is moved successfully, a commit is performed:

<!--SCRIPT 
success = false;
query = connection.CreateQuery( "select amount from savings where id = '99999999'" );
if( query != null ) {
balance = query.GetValue(1) - 100;
query.Close();
query = connection.CreateQuery( "update savings set amount = '" + balance + "' where id = '99999999'" );
if( query != null ) {
query.Close();
query = connection.CreateQuery( "select amount from chequing where id = '99999999'" );
if( query != null ) {
balance = query.GetValue(1) + 100;
query.Close();
query = connection.CreateQuery( "update chequing set amount = '" + balance + "' where id = '99999999'" );
if( query != null ) {
query.Close();
success = true;
}
}
}
}
if( success ) {
connection.Commit();
} else {
connection.Rollback();
}
-->

Connect method

Syntax

connection.Connect ( )

Description

Connects to a connection object.

Return

Boolean. Indicates whether the connection was successful or not.

Example

This example connects to the myConn connection:

<!--SCRIPT 
myConn = site.CreateConnection( "myConn","My new ODBC connection", "Dynamo demo","dba","sql","ODBC" );
myConn.Connect();
-->

See also

The "Disconnect method"

CreateComponent method

Syntax

connection.CreateComponent (packageName, componentName )

Description

This method is for calling Jaguar components. connection.CreateComponent returns a component object representing the object referenced within the package.

Return

The component object or null.

Example

This example calls a Jaguar component and uses its setMessage method:

<!--SCRIPT 
myJagComponent = myJaguarconnection.CreateComponent( "Demo_Components", "SharedMsg" );
myQueryObject = myJagcomponent.setMessage( message );
-->

CreateQuery method

Syntax

connection.CreateQuery ( [ SQLStatement, [doExecute ]] )

Description

Executes a query containing a sqlStatement and returns a query object (equivalent to performing myQuery.SetSQL and myQuery.Execute ).

You can use CreateQuery method in these ways:

Return

Returns a query object.

Example

This example queries the sample database (using the connection "sample") for a list of all the employees and then displays the list in alphabetical order:

<!--SCRIPT 
// Create a query to display first and last name
// of employees in alphabetical order
myConn=site.GetConnection( "sample" );
if ( myConn != null ) {
myQuery = myConn.CreateQuery( "select emp_lname, emp_fname from employee order by emp_lname, emp_fname" );
if ( myQuery.GetErrorCode != 0 )
while( myQuery.MoveNext() ) {
lastName = myQuery.GetValue(1);
firstName = myQuery.GetValue(2);
document.WriteLn( lastName + ", " + firstName );
}
}
-->

This example has the following output:

Ahmed, Alex
Barker, Joseph
Barletta, Irene
Bertrand, Jeannette
Bigelow, Janet
Blaikie, Barbara
Braun, Jane
Breault, Robert
Bucceri, Matthew
Butterfield, Joyce
Chao, Shih Lin
Charlton, Doug
Chin, Philip
Clark, Alison
Cobb, Matthew
Coe, Kristen
Evans, Scott
...

See also

"query object".

Disconnect method

Description

Disconnects a connection object from a database.

Syntax

connection.Disconnect ( )

Example

This example disconnects from a connection called myConn:

<!--SCRIPT 
myConn = site.CreateConnection( "myConn","My new ODBC connection", "Dynamo demo","dba","sql","ODBC" );
myConn.Disconnect();
-->

GetErrorCode method

Syntax

connection.GetErrorCode( )

Description

Returns the current error code.

Return

Integer. Returns zero if a SQL instruction is carried out correctly, and information to which the script can respond if it fails.

Example

This example displays a 0 if the query executes without error:

<HTML> 
<TITLE>sample.stm</TITLE>
<BODY>
<H1></H1>
<!--SQL
SELECT customer.fname, customer.lname, customer.phone, customer.id
FROM DBA.customer customer
-->

<!--SCRIPT
queryError = connection.GetErrorCode();
document.WriteLn( queryError );
-->
</BODY>
</HTML>

See also

"query object".

The "query properties".

The "GetErrorInfo method".

The "GetState method".

GetErrorInfo method

Syntax

connection.GetErrorInfo( )

Description

A description of the error.

Return

String.

Example

This example displays information explaining why the query did not work correctly:

<HTML> 
<TITLE>sample.stm</TITLE>
<BODY>
<H1></H1>
<!--SQL
SELECT customer.fname, customer.lname, customer.phone, customer.id
FROM DBA.customer customeer
-->

<!--SCRIPT
queryState = connection.GetState();
document.WriteLn( queryState );
queryInfo = connection.GetErrorInfo();
document.WriteLn( queryInfo );
-->
</BODY>
</HTML>

See also

"query object".

The "query properties".

"GetErrorCode method".

"GetState method".

GetState method

Syntax

connection.GetState( )

Description

Returns the current SQL state of the query.

The values returned by GetState depend on your database driver. For more information, see your database's documentation.

Example

This example displays the SQL state for the connection:

<HTML> 
<TITLE>sample.stm</TITLE>
<BODY>
<H1></H1>
<!--SQL
SELECT customer.fname, customer.lname, customer.phone, customer.id
FROM DBA.customer customeer
-->

<!--SCRIPT
queryState = connection.GetState();
document.WriteLn( queryState );
queryInfo = connection.GetErrorInfo();
document.WriteLn( queryInfo );
-->
</BODY>
</HTML>

See also

"query object".

The "query properties".

"GetErrorCode method".

"GetErrorInfo method".

GetSupportedMoves method

Syntax

connection.GetSupportedMoves ( )

Description

Provides a list of Move methods that can be called for queries using that particular connection.

Example

This example displays the available Move methods for the connection connection:

<!--SCRIPT 
moves = connection.GetSupportedMoves();
i = 0;
while( exists( moves[i] ) ) {
document.writeln( moves[i] );
i++;
}
-->

If this was a connection to Adaptive Server Anywhere, the output would be:

MoveFirst
MoveNext
MovePrevious
MoveRelative
Move
MoveLast

Rollback method

Syntax

connection.Rollback ( )

Description

Performs a rollback on the transaction.

Return

Boolean.

Example

This example moves 100 dollars from a checking account to a savings account. If the money is not moved successfully, a rollback is done:

<!--SCRIPT 
success = false;
query = connection.CreateQuery( "select amount from savings where id = '99999999'" );
if( query != null ) {
balance = query.GetValue(1) - 100;
query.Close();
query = connection.CreateQuery( "update savings set amount = '" + balance + "' where id = '99999999'" );
if( query != null ) {
query.Close();
query = connection.CreateQuery( "select amount from chequing where id = '99999999' );
if( query != null ) {
balance = query.GetValue(1) + 100;
query.Close();
query = connection.CreateQuery( "update chequing set amount = '" + balance + "' where id = '99999999'" );
if( query != null ) {
query.Close();
success = true;
}
}
}
if( success ) {
connection.Commit();
} else {
connection.Rollback();
}
-->

 


Copyright © 1999 Sybase, Inc. All rights reserved.