Due to popular demand, are we reposting our flying dataset
MyComponents Quick Tip from almost a year back.
Many times in development, do you find yourself just quickly looking for a dataset containing something from the database. Usually will such a dataset not be connected to visual controls. TMySQLServer is capable of creating, maintaining and destroying just such dynamic "in-memory" datasets. These datasets closely resemble ADO or ASP style coding and each dataset is referenced by a userdefined name. You can even connect such datasets up master-detail. The aim is of course to minimize the number of lines you have to write and your effort in general. Here are a couple of examples:
Code:
Example 1 (Usage):
...
with TMySQLDataset(MySQLServer1.Dataset) do begin
ShareConnection := False;
TableName := 'accounts';
end;
MySQLServer1.Dataset.Active := True;
...
Quote:
This will create and return a default closed no name dataset
If you reference it again you will get the same dataset
Code:
Example 2 (Quickie):
...
MyNo := MySQLServer1.DatasetFrom('@set No=23; select (Max(AccountID)+No) as MyCount from accounts').FieldByName('MyCount').AsInteger;
MySQLServer1.FreeDataset;
...
Quote:
The dataset has no name, so freedataset will automatically destroy the dataset without a name.
Code:
Example 3 (Master-Detail):
...
MySQLServer1.Dataset('MyAccounts','accounts',True);
// This will create a dataset called MyAccounts opening it on mysql table "accounts" in the current database
MySQLServer1.Dataset'MyOrders','orders',True,'MyAccounts','OrderAccountID=AccountID');
// This will create a 2nd dataset called "MyOrders" opening it on mysql table "orders" in the current database
// It will automatically select all orders where the order's OrderAccountID is equal to the AccountID of the current record in the MyAccounts dataset. If MyAccounts has no records, then MyOrders won't have any records either.
MySQLServer1.DatasetFrom('select * from orderdetails where ProductID=10','MyOrderDetails',False,'MyOrders','DetailOrderID=OrderID').TableName := 'orderdetails';
// This will automatically create a fully updateble dataset using a SQL statement, open it and master-detail it to the MyOrders dataset where MyOrderDetails' DetailOrderID=OrderID of the MyOrders table.
// Further more all inserts or appends done on any detail dataset will automatically get their linked column/field populated with the value of the linked column in the master dataset. So you really don't have to do much.
with MySQLServer1.Dataset('MyOrderDetails') do begin
Open;
Append;
FieldByName('ProductDesc').AsString := 'MyComponents';
Post;
end;
// We just create a three tier master-detailing dataset relationship, have 3 fully updateble datasets on hand and we did in 3 lines of code
// And it took another 3 lines of code to append a new record to the last dataset and have it inserted a new record in the mysql table, populate it's linked field with the masters's current value, without you worrying about a thing.
// We can keep them all like this for further usage or get the memory back as easy as this
MySQLServer1.FreeDataset('MyAccounts');
MySQLServer1.FreeDataset('MyOrders');
MySQLServer1.FreeDataset('MyOrderDetails');
...
Enjoy, Support