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.

<strong>C++</strong>Builder Database Architecture<br />

Updates that you make via the DeleteSQL and ModifySQL statements will, of course, need to<br />

be qualified by a SQL WHERE clause. Both of these properties support a special extension to<br />

SQL that enables you to refer to a field’s orig<strong>in</strong>al value by prefix<strong>in</strong>g its name with Old_. This<br />

is similar to InterBase’s Old. context variable. For example, the ModifySQL statement you set<br />

up for the CUSTOMER table might look like this:<br />

UPDATE CUSTOMER SET Name=:Name<br />

WHERE CustomerNo=:Old_CustomerNo<br />

Though this query doesn’t actually change CustomerNo, it’s a good idea to get <strong>in</strong>to the habit<br />

of us<strong>in</strong>g the Old_ prefix anyway, because some updates aren’t possible without it.<br />

TIP<br />

When edit<strong>in</strong>g the SQL associated with the TUpdateSQL component, you<br />

can edit your SQL us<strong>in</strong>g <strong>C++</strong>Builder’s full-blown Code Editor. You do<br />

this by click<strong>in</strong>g the Code Editor button from with<strong>in</strong> the InsertSQL,<br />

DeleteSQL, and ModifySQL property editors. You’ll f<strong>in</strong>d <strong>C++</strong>Builder’s<br />

Code Editor to be much more powerful than the TMemo component<br />

that’s used to edit the three properties by default.<br />

Use your TDataset’s OnUpdateRecord event when you want to perform additional process<strong>in</strong>g<br />

before send<strong>in</strong>g rows to TUpdateSQL. Once you’ve completed this additional process<strong>in</strong>g, you<br />

can call TUpdateSQL’s Apply method to replace the parameters embedded <strong>in</strong> your SQL and<br />

execute the SQL aga<strong>in</strong>st your database. Here’s a sample OnUpdateRecord handler:<br />

void __fastcall TForm1::Table1UpdateError(TDataset *TDataset, EDatabaseError *E,<br />

TUpdateK<strong>in</strong>d,TUpdateAction &UpdateAction)<br />

{<br />

#def<strong>in</strong>e DEFAULTRATE 5.00<br />

if (UpdateK<strong>in</strong>d == ukInsert)<br />

StoredProc1->FieldByName(“Rate”)->Value=DEFAULTRATE;<br />

UpdateSQL1->Apply(UpdateK<strong>in</strong>d);<br />

UpdateAction=uaApplied;<br />

}<br />

Notice that the rout<strong>in</strong>e sets the UpdateAction parameter to tell the cached updates ApplyUpdates<br />

rout<strong>in</strong>e that no further action is necessary.<br />

TField<br />

Module: DBTables Class ancestor: TComponent<br />

The TField component is used to access the columns <strong>in</strong> a TDataset’s rows. Everyth<strong>in</strong>g that<br />

<strong>C++</strong>Builder enables you to configure at the field level is done with the TField component.<br />

467<br />

12

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

Saved successfully!

Ooh no, something went wrong!