Podstrony
- Strona startowa
- Borland Delphi 7 Developer's Guide
- Borland C Builder 6 Book
- Linux Programming Unleashed
- Weber Dav
- Stanislaw Lem Eden (4)
- Bourdais Gildas UFO, 50 tajemniczych lat
- Rowling J K Harry Potter i wiezien Azkabanu
- Cornwell Bernard Trylogia Arturiańska Tom 1 Zimowy Monarcha (3)
- Hobb Robin Krolewski Skrytobojca
- Masterton Graham Studnie piekiel (2)
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- tohuwabohu.xlx.pl
[ Pobierz całość w formacie PDF ]
.PAS.The TDataSet object is a beautifully written wrapper around the core functionalityin the BDE.After reading the last two sections, you should have a good feeling for how to move around in a dataset.The navigationalcommands you have been learning about are very easy to use.Take the few moments necessary to be sure you understand them,because they are likely to be part of your day-to-day BCB programming experience.FieldsOn most occasions when you want to programmatically access the individual fields of a record, you can use one of the followingproperties or methods, all of which belong to TDataSet:__property TField *Fields[System::Integer Index];__property System::Integer FieldCount;__property System::Variant Value;TField *__fastcall FieldByName(const System::AnsiString FieldName);The Fields property is one of the most important single syntactical elements of the VCL.It consists of an array of TFieldobjects, each of which is automatically assigned to a separate field in a table.For instance, if a table called Address had five fieldscalled Name, Address, City, State and Zip, BCB would automatically create five field objects when you accessed theAddress table.You don't have to do anything to create these field objects; they are given to you automatically.The Fields array for the hypothetical Address table mentioned in the last paragraph would have five members.Fields[0]would access the Name field, Fields[1] would access the Address field, and so on.The TField object is declared in DB.HPP.This is an extremely powerful, and fairly complex object, which is worth your while tofile:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch09.htm (6 of 36) [10/10/2000 1:13:07 AM]Ch 9 -- Using TTable and TDataSetstudy in depth.For instance, here are some of the methods of the TField object:Assign Assigns one field to another.Clear Sets the field to NULL.FocusControl Sets the focus of the form to the first control that hosts the field.GetData Gets raw data from a field in a buffer.Contrast with AString, AsInteger.SetData Sets the field to raw data held in a pointer.Contrast with AsString or AsInteger.IsValidChar Tests to see if a character is within the range of valid values for a particular field.Here are some of the properties associated with a TField object:AsBoolean Conversion property, can be used to read or set the value of a field as a Boolean value.AsDateTime Conversion property, can be used to read or set the value of a field as a date.AsFloat Conversion property, can be used to read or set the value of a field as Float.AsString Conversion property, can be used to read or set the value of a field as a string.AsInteger Conversion property, can be used to read or set the value of a field as an Integer.Calculated Read only Boolean property, tells whether a field is calculated.DataSet Assign a dataset to a field, or read what dataset is associated with a field.EditMask Define a mask limiting the valid characters used by a field.Value The standard means for accessing the value of a field.FieldName The name of the underlying field in the database.Visible Boolean property toggles whether or not a field is visible.Most of the properties and methods listed previously are discussed in the next few pages of this book.For now you can just reviewthe functionality previously outlined in a general way, so that you can get some sense of what the TField class is about.Remember, the previous list is far from exhaustive.It is meant merely to introduce some of the key features of the object.The TField object also has a number of useful descendant classes with names such as TStringField and TIntegerField.These child objects are discussed in Chapter 17, "Printing: QuickReport and Related Technologies."The FieldCount property returns an integer that specifies the number of fields in the current record structure.If you wanted aprogrammatic way to read the names of these fields, you could use the Fields property:{AnsiString S(Table->Fields[0]->FieldName);}If a record had a first field called CustNo, the preceding code would put the string "CustNo" in the variable S.If you wanted toaccess the name of the second field in the example, you could write this:S = Table->Fields[1]->FieldName;In short, the index passed to Fields is zero-based, and it specifies the number of the field you want to access, where the first fieldis number zero, the second is referenced by the number one, and so on.If you want to find out the current contents of a particular field from a particular record, you can use the Fields orFieldByName property, or you could access the entire table as an array of fields.To find the value of the first field of a record,index into the first element of the Fields array:S = Table->Fields[0]->AsString;Assuming that the first field in a record contains a customer number, the code shown would return a string such as `1021', `1031',or `2058'.If you wanted to access this variable as an integer value, you could use AsInteger in place of AsString.Similarproperties of Fields include AsBoolean, AsFloat, and AsDate.If you want, you can use the FieldByName function instead of the Fields property:S = Table->FieldByName("CustNo")->AsString;file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch09.htm (7 of 36) [10/10/2000 1:13:07 AM]Ch 9 -- Using TTable and TDataSetAs used in the examples shown here, both FieldByName and Fields return the same data.The two different syntaxes are usedsolely to provide programmers with a flexible and convenient set of tools for programmatically accessing the contents of a dataset.When in doubt, use FieldByName because it won't be affected if you change the order of the fields in your table.NOTE: I'll add a note here for Delphi programmers who may be a bit confused by the tack I am taking on this subject.In Delphi, you can also treat TTable as a variant array, which will let you access the fields of a table with thefollowing syntax:S := Table1[`CustNo'];This is obviously a considerable improvement over the FieldByName method.However, this syntax is not supportedin BCB.It might be helpful to take a few moments to discuss a set of routines from the FieldObject program found on the disk thataccompanies this book.These routines illustrate the most common ways to access the value of a field.The vValueClick routine shows the default means for setting or getting the value of a field:void __fastcall TForm1::bValueClick(TObject *Sender){ShowMessage(tblOrders->FieldByName("TaxRate")->Value);}This is the standard way to get at a field.You use the AsString or AsInteger properties for conversion, but when you justwant to get at the type of a field, you can use the Value property.However, the issue of conversion can sometimes raise its head atstrange moments, so it is helpful to note that properties such as AsInteger and AsString exist.In the TField object, the Value property is declared as being of type Variant, which means it will handle most typeconversions for you automatically.This is illustrated in the previous example, where the TaxRate field, which is of type Float,is converted for you automatically to a string
[ Pobierz całość w formacie PDF ]