1095-1096

Previous Table of Contents Next

Page 1095

The example in Listing 48.6 assumes that the environment and connection handle, hEnv and hDBc, are valid and connected to the data source, and that szSQL is an array of iStmts SQL statements. If any statement in the transaction fails, a rollback is issued and processing of the transaction is discontinued. If all statements are processed without errors, the entire transaction is committed. Regardless of whether the transaction is committed or rolled back, the application frees the statement handle.

When the application exits, it needs to disconnect from the data source and free all resources allocated to the environment and connection handles. This can be accomplished with three functions calls, as illustrated in the following lines:

 iError = SQLDisconnect(hDB) iError = SQLFreeConnect(hDB) iError = SQLFreeEnv(hEnv) 

The full capabilities of ODBC are far beyond the scope of this discussion, but many additional capabilities can be provided, and an application can be constructed in a manner that is completely database-independent using embedded SQL. However, if an application does not need to be portable to other database platforms, it may be easier to use stored procedures or a third-party product, such as Oracle Objects for OLE, to apply transactions from Visual Basic.

It is unfortunate that Visual Basic does not provide a means by which AutoCommit can be disabled for connections established internally. This is a limitation of the implementation of the Jet Database Engine that Visual Basic uses.

One of the most powerful (and potentially dangerous) features of Visual Basic is the means by which errors are trapped, using the On Error statement. This statement is well used in Visual Basic programming because all untrapped runtime errors are fatal. The On Error statement is used to specify an error handler to which execution jumps if an error occurs. The error handler can be specified using GoTo, or Resume Next can be specified to allow program execution to continue normally. The system variables Err and Error$ store a numeric code and a text description for the error that occurred most recently. The fact that only the most recent value is stored is what makes On Error Resume Next a somewhat dangerous way to trap and handle errors. It is very convenient to use, but the value of Err should be checked wherever an error requiring a specific action occurs.

A common mistake in Visual Basic programming is to misunderstand its array dimensions. Consider the following declaration, for example:

 ReDim szStrings(2) As String 

Most would assume that this declares an array of two strings. However, the previous statement actually allocates an array of three strings. The subscript in an array declaration sets the upper bound, not the number of elements in the array, so szString(0), szString(1), and szString(2) are all valid, based on the declaration. The base subscript can be changed to 1, using the Option Base statement:

 Option Base 1 

Page 1096

If this statement appears in the same module as the previous array declaration, the array would have only two elements, szString(1) and szString(2). In this case, szString(0) would be an invalid reference.

Summary

The Visual Basic programming language is particularly easy to learn and use. Nearly all developers have some experience with BASIC, and many of the language constructs have been in place since the very first versions of what is now referred to as BASIC A.

Despite some idiosyncrasies, a lack of object-oriented features, and relatively weak database support, Visual Basic is an extremely useful development tool. Its shortcomings are offset by the simplicity of its IDE and language, and extensive third-party support. The uncluttered and completely nonmodal IDE, a flexible ANSI-standard programming language, and a wide variety of prebuilt objects make Visual Basic an excellent choice for projects requiring rapid development.

Previous Table of Contents Next


Oracle Unleashed
Oracle Development Unleashed (3rd Edition)
ISBN: 0672315750
EAN: 2147483647
Year: 1997
Pages: 391

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net