12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

440 Day 12<br />

NOTE<br />

Don’t confuse TDatabase’s KeepConnections property with TSession’s<br />

property of the same name. TSession’s property affects only temporary<br />

TDatabase components, not those you create. Sett<strong>in</strong>g TSession’s<br />

KeepConnections property will have no effect on whether your explicit<br />

TDatabase connections are reta<strong>in</strong>ed.<br />

Chang<strong>in</strong>g Server Log<strong>in</strong> Parameters<br />

You can use TDatabase’s OnLog<strong>in</strong> event handler to keep the default password dialog box from<br />

display<strong>in</strong>g when a connection is <strong>in</strong>itiated. OnLog<strong>in</strong> gets passed two parameters: a TDatabase<br />

component that po<strong>in</strong>ts to the database the user is try<strong>in</strong>g to log on to, and a TStr<strong>in</strong>gs object<br />

for stor<strong>in</strong>g the required log<strong>in</strong> parameters. Here’s the header def<strong>in</strong>ition for a typical OnLog<strong>in</strong><br />

event method handler:<br />

void __fastcall TForm1::Database1Log<strong>in</strong>(TDatabase *Database, TStr<strong>in</strong>gs<br />

*Log<strong>in</strong>Params)<br />

From <strong>in</strong>side the OnLog<strong>in</strong> method handler, you can use TStr<strong>in</strong>gs’s <strong>in</strong>dexed Values property<br />

to access <strong>in</strong>dividual parameters, like so:<br />

Log<strong>in</strong>Params->Values[“SERVER NAME”] = “VH1”;<br />

Log<strong>in</strong>Params->Values[“USER NAME”] = “dave”;<br />

Log<strong>in</strong>Params->Values[“PASSWORD”] = “ureally”;<br />

To prevent the default log<strong>in</strong> dialog box from display<strong>in</strong>g, you’ll have to at least set the PASSWORD<br />

parameter. You can gather the parameters you need from a dialog box of your own, retrieve<br />

them from another Database component, or hard-code them—it doesn’t matter. If they leave<br />

your OnLog<strong>in</strong> method handler with values, <strong>C++</strong>Builder will attempt to use them to establish<br />

a connection.<br />

Application-Controlled Transaction Process<strong>in</strong>g<br />

A transaction is a unit of database work—a set of data modifications that you want to treat<br />

as a s<strong>in</strong>gle unit. It may consist of a s<strong>in</strong>gle data-modification command; it may consist of<br />

thousands. When you group a series of data changes together as a s<strong>in</strong>gle transaction, you<br />

ensure that either all the changes occur or none of them do. If a transaction consists of 1,000<br />

changes and the 999th one fails, they all fail, and the database behaves as though none of them<br />

ever occurred. Normally, <strong>C++</strong>Builder handles transaction-related issues for you automatically<br />

by start<strong>in</strong>g and committ<strong>in</strong>g transactions when your application attempts to make<br />

changes to a database. If this level of control isn’t sufficient, you can guide transaction<br />

process<strong>in</strong>g yourself us<strong>in</strong>g the TransIsolation property and the StartTransaction, Commit,<br />

and Rollback methods.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!