[ Pobierz całość w formacie PDF ]
.You ll need at least to call its ApplyUpdates method to send theactual updates back to the database server (through the provider)." The DataSource component allows you to surface the data from the ClientDataSet tothe visual data-aware controls.As I mentioned earlier, the picture can be simplified by using the SQLClientDataSet, whichreplaces the two datasets and the provider.The SQLClientDataSet combines most of the prop-erties of the components it replaces.For a simple example, you ll have to set the DBConnectionproperty for connecting to the proper database, the CommandTypeand CommandTextpropertiesto specify which data to fetch, and the PacketRecordsproperty to indicate how many records toretrieve in each block.You ll also need to call to the ApplyUpdatesmethod to send the actualupdates back to the database.These are the key properties of the core components of the DbxSingle example:object SQLConnection1: TSQLConnectionConnectionName = IBLocalLoginPrompt = Falseendobject SQLClientDataSet1: TSQLClientDataSetCommandText = EMPLOYEECommandType = ctTableDBConnection = SQLConnection1endAs an alternative, the DbxMulti example uses the entire sequence of components:object SQLConnection1: TSQLConnectionConnectionName = IBLocalLoginPrompt = Falseendobject SQLDataSet1: TSQLDataSetSQLConnection = SQLConnection1CommandText = select * from EMPLOYEEendobject DataSetProvider1: TDataSetProviderDataSet = SQLDataSet1endobject ClientDataSet1: TClientDataSetProviderName = DataSetProvider1endobject DataSource1: TDataSourceDataSet = ClientDataSet1endCopyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com2874c14.qxd 7/2/01 4:34 PM Page 634634 Chapter 14 " Client/Server ProgrammingBoth examples also have some visual controls: a grid and a toolbar based on the actionmanager architecture.Applying UpdatesWhat is important to do in every example based on a local cache, like the one provided by theClientDataSet and SQLClientDataSet components, is to write the local changes back to thedatabase server.This is typically accomplished by calling the ApplyUpdatesmethod.But whenshould you call it? You can either keep the changes in the local cache for a while and then applya bunch of updates at once, or post each change right away.In these two simple examples, I vegone for the latter approach, attaching the following event handler to the AfterPost(fired afteran edit or an insert operation) and AfterDeleteevents of the ClientDataSet components:procedure TForm1.DoUpdate(DataSet: TDataSet);begin// immediately apply local changes to the databaseSQLClientDataSet1.ApplyUpdates(0);end;If you want to apply all the updates in a single batch, you can either do this when the formis closed or the program ends, or let a user do the update operation by selecting a specificcommand.We ll explore some of these alternatives when discussing the ClientDataSet com-ponent in more detail.Monitoring the ConnectionAnother feature, which I ve added only to the DbxSingle example, is the monitoring capabilityoffered by the SQLMonitor component.In the example, the component is activated as theprogram starts.Every time there is a tracing string available, the component fires the OnTrace event to letyou choose whether to include the string in the log.If the LogTrace parameter of this event isTrue (the default value), the component logs the message in the TraceList string list andfires the OnLogTrace event to indicate that a new string has been added to the log.The component can also automatically store the log into the file indicated by its FileNameproperty, but I haven t used this feature in the example.All I ve done is to handle the OnLogTraceevent, adding the last message to a memo component with the following code (and the out-put of Figure 14.9):procedure TForm1.SQLMonitor1LogTrace(Sender: TObject;CBInfo: pSQLTRACEDesc);beginMemoLog.Lines.Add (CBInfo.pszTrace);end;Copyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com2874c14.qxd 7/2/01 4:34 PM Page 635The dbExpress Library 635FI GURE 14.9:A sample log obtained bythe SQLMonitor in theDbxSingle example.Also, a button allows a user to refresh the memo with the entire content of the trace list:procedure TForm1.Button1Click(Sender: TObject);beginMemoLog.Lines := SQLMonitor1 [ Pobierz caÅ‚ość w formacie PDF ]
zanotowane.pl doc.pisz.pl pdf.pisz.pl odbijak.htw.pl
.You ll need at least to call its ApplyUpdates method to send theactual updates back to the database server (through the provider)." The DataSource component allows you to surface the data from the ClientDataSet tothe visual data-aware controls.As I mentioned earlier, the picture can be simplified by using the SQLClientDataSet, whichreplaces the two datasets and the provider.The SQLClientDataSet combines most of the prop-erties of the components it replaces.For a simple example, you ll have to set the DBConnectionproperty for connecting to the proper database, the CommandTypeand CommandTextpropertiesto specify which data to fetch, and the PacketRecordsproperty to indicate how many records toretrieve in each block.You ll also need to call to the ApplyUpdatesmethod to send the actualupdates back to the database.These are the key properties of the core components of the DbxSingle example:object SQLConnection1: TSQLConnectionConnectionName = IBLocalLoginPrompt = Falseendobject SQLClientDataSet1: TSQLClientDataSetCommandText = EMPLOYEECommandType = ctTableDBConnection = SQLConnection1endAs an alternative, the DbxMulti example uses the entire sequence of components:object SQLConnection1: TSQLConnectionConnectionName = IBLocalLoginPrompt = Falseendobject SQLDataSet1: TSQLDataSetSQLConnection = SQLConnection1CommandText = select * from EMPLOYEEendobject DataSetProvider1: TDataSetProviderDataSet = SQLDataSet1endobject ClientDataSet1: TClientDataSetProviderName = DataSetProvider1endobject DataSource1: TDataSourceDataSet = ClientDataSet1endCopyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com2874c14.qxd 7/2/01 4:34 PM Page 634634 Chapter 14 " Client/Server ProgrammingBoth examples also have some visual controls: a grid and a toolbar based on the actionmanager architecture.Applying UpdatesWhat is important to do in every example based on a local cache, like the one provided by theClientDataSet and SQLClientDataSet components, is to write the local changes back to thedatabase server.This is typically accomplished by calling the ApplyUpdatesmethod.But whenshould you call it? You can either keep the changes in the local cache for a while and then applya bunch of updates at once, or post each change right away.In these two simple examples, I vegone for the latter approach, attaching the following event handler to the AfterPost(fired afteran edit or an insert operation) and AfterDeleteevents of the ClientDataSet components:procedure TForm1.DoUpdate(DataSet: TDataSet);begin// immediately apply local changes to the databaseSQLClientDataSet1.ApplyUpdates(0);end;If you want to apply all the updates in a single batch, you can either do this when the formis closed or the program ends, or let a user do the update operation by selecting a specificcommand.We ll explore some of these alternatives when discussing the ClientDataSet com-ponent in more detail.Monitoring the ConnectionAnother feature, which I ve added only to the DbxSingle example, is the monitoring capabilityoffered by the SQLMonitor component.In the example, the component is activated as theprogram starts.Every time there is a tracing string available, the component fires the OnTrace event to letyou choose whether to include the string in the log.If the LogTrace parameter of this event isTrue (the default value), the component logs the message in the TraceList string list andfires the OnLogTrace event to indicate that a new string has been added to the log.The component can also automatically store the log into the file indicated by its FileNameproperty, but I haven t used this feature in the example.All I ve done is to handle the OnLogTraceevent, adding the last message to a memo component with the following code (and the out-put of Figure 14.9):procedure TForm1.SQLMonitor1LogTrace(Sender: TObject;CBInfo: pSQLTRACEDesc);beginMemoLog.Lines.Add (CBInfo.pszTrace);end;Copyright ©2001 SYBEX, Inc., Alameda, CA www.sybex.com2874c14.qxd 7/2/01 4:34 PM Page 635The dbExpress Library 635FI GURE 14.9:A sample log obtained bythe SQLMonitor in theDbxSingle example.Also, a button allows a user to refresh the memo with the entire content of the trace list:procedure TForm1.Button1Click(Sender: TObject);beginMemoLog.Lines := SQLMonitor1 [ Pobierz caÅ‚ość w formacie PDF ]