⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch16.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
types of databases. These drivers translate high-level database commands (such asopen or post) into commands specific to a particular database type. This permitsyour application to connect to a database without needing to know the specifics ofhow that database works.</P><P>The drivers that are on your system depend on the version of Delphi you own. Allversions of Delphi come with a driver to enable you to connect to Paradox and dBASEdatabases. This driver, called STANDARD, provides everything you need to work withthese local databases.</P><P>The Client/Server version of Delphi includes drivers to connect to databases bySybase, Oracle, Informix, InterBase, and others.</P><P><H3><A NAME="Heading7"></A>BDE Aliases</H3><P>The BDE uses an alias to access a particular database. This is one of those termsthat might confuse you at first. The terms <I>alias</I> and <I>database</I> are oftenused interchangeably when talking about the BDE.</P><P><P><strong>New Term:</strong> A <I>BDE alias</I> is a set of parameters that describes adatabase connection.</P><PRE></PRE><P>When it comes right down to it, there isn't much to an alias. In its simplestform, an alias tells the BDE which type of driver to use and the location of thedatabase files on disk. This is the case with aliases you will set up for a localdatabase. In other cases, such as aliases for client/server databases, the aliascontains other information as well, such as the maximum size of BLOB data, the maximumnumber of rows, the open mode, or the user's username. After you create an aliasfor your database, you can use that alias to select the database in your Delphi programs.Later today, in the section &quot;Creating a BDE Alias,&quot; I'll tell you how togo about creating a BDE alias for your own databases.</P><P><H3><A NAME="Heading8"></A>Delphi's Built-in Databases</H3><P>As long as I am on the subject of aliases, let's take a quick look at the aliasesalready set up on your system. To view existing aliases, perform these steps:</P><DL>	<DT></DT>	<DD><B>1. </B>Start Delphi or create a new application if Delphi is already running.	<P>	<DT></DT>	<DD><B>2. </B>Switch to the Data Access tab of the Component palette, select a Table	component, and place it on the form.	<P>	<DT></DT>	<DD><B>3. </B>Click on the DatabaseName property in the Object Inspector and then	click the drop-down arrow button to display a list of aliases.	<P></DL><P>After performing these steps, you'll see a list of available databases. At leastone of these should be the DBDEMOS alias. This database alias is set up when Delphiis installed. Select the DBDEMOS database from the list.</P><P><strong>NOTE:</strong> The list of databases you see depends on several factors. First,it depends on whether you have the Standard, Professional, or Client/Server versionof Delphi. It also depends on whether you elected to install Local InterBase. Finally,if you happen to have C++Builder or another Borland product installed (such as VisualdBASE or IntraBuilder), you might see additional databases.</P><P>As long as you are here, move to the TableName property and take a look at theavailable tables. The tables you see are those available for this database (thisalias). Select another alias for the DatabaseName property. Now look at the tablenames again. You will see a different list of tables.</P><P><H3><A NAME="Heading9"></A>SQL Links</H3><P>The Client/Server version of Delphi comes with SQL Links in addition to the BDE.SQL Links is a collection of additional drivers for the BDE. These drivers enableDelphi applications to connect to client/server databases such as those providedby Oracle, InterBase, Informix, Sybase, and Microsoft. Details regarding deploymentof SQL Links drivers are also available in DEPLOY.TXT.</P><BLOCKQUOTE>	<P><BR>	<HR><B>LOCAL INTERBASE</B></P>	<P>The Standard and Professional versions of Delphi come with a single-user copy	of Local InterBase. Local InterBase is just what its name implies: a version of InterBase	that operates on local databases. The Client/Server version of InterBase, on the	other hand, is a full-featured client/server database. The main reason that Delphi	ships with Local InterBase is so that you can write an application that operates	on local databases and then later change to a client/server database with no programming	changes. This gives you an opportunity to hone your client/server programming skills	without spending the money for a client/server database.</P>	<P>If you attempt to access a Local InterBase table at either design time or runtime,	you will be prompted for a username and password. The Local InterBase administrator	is set up with a username of SYSDBA and a password of masterkey. You can use these	for login, or you can go to the InterBase Server Manager utility and add yourself	as a new user to the InterBase system. <HR></BLOCKQUOTE><H2><A NAME="Heading10"></A>Delphi Database Components</H2><P>Okay, so the preceding section isn't exactly the type of reading that keeps youup all night turning pages. Still, it's important to understand how all the databasepieces fit together. With that background, you can now turn your attention to thedatabase components provided by VCL and how those components work together to createa database application. First, I'll give you a quick overview of the VCL databasecomponents, and then you'll look at individual classes and components in more detail.</P><PRE></PRE><P>The VCL database components fall into two categories: nonvisual data access componentsand visual data-aware components. Simply put, the nonvisual data access componentsprovide the mechanism that enables you to get at the data, and the visual data-awarecomponents enable you to view and edit the data. The data access components are derivedfrom the TDataSet class and include TTable, TQuery, and TStoredProc. The visual data-awarecomponents include TDBEdit, TDBListBox, TDBGrid, TDBNavigator, and more. These componentswork much like the standard edit, list box, and grid components except that theyare tied to a particular table or field in a table. By editing one of the data-awarecomponents, you are actually editing the underlying database as well.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> All the VCL database components can be termed data components. I	use the term <I>data access components</I> for the nonvisual database components	on the Data Access tab of the Component palette and the term <I>data-aware components</I>	for the visual database components from the Data Controls tab. <HR></BLOCKQUOTE><P>Interestingly, these two component groups cannot talk directly to each other.Instead, the TDataSource component acts as an intermediary between the TDataSet componentsand the visual data-aware components. This relationship is illustrated in Figure16.3.</P><P><A HREF="javascript:popUp('28671603.gif')"><B>FIGURE 16.3.</B></A><B> </B><I>Thearchitecture of the VCL database components.</I></P><P>You'll look at these components in more detail, but first I'll walk you througha quick exercise to illustrate the relationship described in this section. StartDelphi or create a new application if Delphi is already running. Now do the following:</P><DL>	<DD><B>1. </B>Place a Table component on the form.	<DT></DT>	<DD><B>2. </B>Locate the DatabaseName property in the Object Inspector and choose	the DBDEMOS database.	<P>	<DT></DT>	<DD><B>3. </B>Locate the TableName property and choose the ANIMALS.DBF table.	<P>	<DT></DT>	<DD><B>4. </B>Drop a DataSource component on the form and set its DataSet property	to Table1 (choose Table1 from the drop-down list). The data source is now linked	to the dataset (the Table).	<P>	<DT></DT>	<DD><B>5. </B>Drop a DBGrid component on the form and change its DataSource property	to DataSource1. This connects the grid to the data source and, indirectly, to the	dataset.	<P>	<DT></DT>	<DD><B>6. </B>Now click the Table component on your form to select it. Change its	Active property to True. You now have data in the table.	<P></DL><P>That was easy, but you're not done yet. Notice, by the way, that you can use thescrollbars on the grid even at design time. Okay, just a couple more steps:</P><DL>	<DT></DT>	<DD><B>1. </B>Place a DBImage component on the form and change its DataSource property	to DataSource1 and its DataField property to BMP (BMP is a field name in the ANIMALS.DBF	table that contains a picture of the animal). Hey, a fish! Size the DBImage as desired	to fit the size of the image that is showing in the component.	<P>	<DD><B>2. </B>Place a DBNavigator component on the form and change its DataSource	property to DataSource1.</DL><P>Now run the program. Click any of the DBNavigator buttons. When you click on theNext Record button, the record pointer changes in the DBTable and the picture changesin the DBImage component. All that without writing a line of code!</P><P>The data access components are used to connect to a database and to a particulartable in a database. The Table component is used to access a database table. Thisis the simplest way of accessing the data in a table.</P><P>The Query component is a way of accessing a database table using Structured QueryLanguage (SQL) statements. SQL is a more powerful way of accessing tables, but itis also more complex. You will use either a Table or Query component to access adatabase, but not both. Another component is the StoredProc component that enablesyou access to a database via stored procedures. A <I>stored</I> <I>procedure</I>is a collection of database statements that performs one or more actions on a database.Stored procedures are usually used for a series of database commands that is repeatedoften.</P><P><H3><A NAME="Heading11"></A>The TDataSet Class</H3><P>TDataSet is the ancestor class for TTable, TQuery, and TStoredProc. As such, mostproperties, methods, and events that these classes use are actually defined by TDataSet.Because so many characteristics of the derived classes come from TDataSet, I'll listthe primary properties, methods, and events of TDataSet here, and later I'll listthe properties, methods, and events particular to each derived class.</P><P>Table 16.1 lists the most commonly used properties of the TDataSet class, Table16.2 lists the primary methods, and Table 16.3 lists the primary events.</P><P><H4>TABLE 16.1. PRIMARY TDataSet PROPERTIES.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Property</I></TD>		<TD ALIGN="LEFT"><I>Description</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Active</TD>		<TD ALIGN="LEFT">Opens the dataset when set to True and closes it when set to False.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">AutoCalcFields</TD>		<TD ALIGN="LEFT">Determines when calculated fields are calculated.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Bof</TD>		<TD ALIGN="LEFT">Returns True if the cursor is on the first record in the dataset and False if it			isn't.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CachedUpdates</TD>		<TD ALIGN="LEFT">When True, updates are held in a cache on the client machine until an entire transaction			is complete. When False, all changes to the database are made on a record-by-record			basis.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">CanModify</TD>		<TD ALIGN="LEFT">Determines whether the user can edit the data in the dataset.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">DataSource</TD>		<TD ALIGN="LEFT">The DataSource component associated with this dataset.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">DatabaseName</TD>		<TD ALIGN="LEFT">The name of the database that is currently being used.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Eof</TD>		<TD ALIGN="LEFT">Returns True if the cursor is at the end of the file and False if it isn't.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">FieldCount</TD>		<TD ALIGN="LEFT">The number of fields in the dataset. Because a dataset might be dynamic (the results			of a query, for example), the number of fields can vary from one dataset request			to the next.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Fields</TD>		<TD ALIGN="LEFT">An array of TFields objects that contains information about the fields in the dataset.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">FieldValues</TD>		<TD ALIGN="LEFT">Returns the value of the specified field for the current record. The value is represented			as a Variant.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Filter</TD>		<TD ALIGN="LEFT">An expression that determines which records a dataset contains.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Filtered</TD>		<TD ALIGN="LEFT">When True, the dataset is filtered based on either the Filter property or the OnFilterRecord			event. When False, the entire dataset is returned.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">FilterOptions</TD>		<TD ALIGN="LEFT">Determines how filters are applied.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Found</TD>		<TD ALIGN="LEFT">Indicates whether a find operation is successful.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Handle</TD>		<TD ALIGN="LEFT">A BDE cursor handle to the dataset. This is used only when making direct calls to			the BDE.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Modified</TD>		<TD ALIGN="LEFT">Indicates whether the current record has been modified.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">RecNo</TD>		<TD ALIGN="LEFT">The current record number in the dataset.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">RecordCount</TD>		<TD ALIGN="LEFT">Returns the number of records in the dataset.</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">State</TD>		<TD ALIGN="LEFT">Returns the current state of the dataset (dsEdit, dsBrowse, dsInsert, and so on).</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">UpdateObject</TD>		<TD ALIGN="LEFT">Specifies the TUpdateObject component to use for cached updates.</TD>	</TR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -