3. Other common objects (objects corresponding to Delphi):
ADODB. Field:tfield ADODB. Parameter:
Tpara ADODB. Error:edbengineerror
Adodb.command: No ADODB. Property: None
Here is an example of an application, listening to others say that you do not see the actual examples to experience. In this example, you will demonstrate how to use ADO objects to query a datasheet, add records, modify records, and delete record operations. Please see the comments in the program, if you have a bit of Delphi database programming experience, I believe it is not difficult to understand.
In our example the database used for Test.mdb, which has a datasheet for Wfjcommu, has five fields aname, portable, Tel, BP, postaddress, respectively, name, cell phone number, phone number, pager number and mailing address.
procedure Tform1.button1click (sender:tobject);
{*****************************************************
using ADO to operate an ODBC database in this program, you will create a temporary ODBC system data source, point to a msaccess database, and then display, add, modify, delete, and query the datasheet in it: Please include the Comobj cell in the uses statement
*****************************************************}
const{Some constant declarations, see adovbs.inc} in detail
{----CommandType constant description----}
adCmdUnknown = 0008;//unknown,
need system to judge, slow, default value
adcmdtext = 0001;//command statements such as SQL statements
adcmdtable = 0002;//Datasheet name
adCmdStoredProc = 0004;//Stored procedure name
{----CursorType constant description----}
adopenforwardonly = 0;//can only be accessed from the front backward one-way, the default value
adOpenKeyset = 1;//visible changes to the data by other users,
but additions and deletions to other users are not visible
adopendynamic = 2;//Other users are visible for additional modifications and deletions to the data
adOpenStatic = 3;//Other users are not visible for additional modifications and deletions to the data
{----LockType constant description---}
adlockreadonly = 1;//read-only, default value
adlockpessimistic = 2;//lock on a single record when modifying
adlockoptimistic = 3;//locks on a single record when updated after modification
adlockbatchoptimistic = 4;//record locks during mass update
var
aconnection, Arecordset variant;
Longinttemp:integer;
strtemp:string;
Intindex:integer;
begin
{Create a temporary ODBC data source, and use this DSN to establish a database connection to a msaccess database}
aconnection: = Createoleobject (ADODB. Connection);
Aconnection.open (driver={microsoft Access Driver
(*.mdb)};D bq=c:\inetpub\wwwroot\test);
{Set up a DataSet object and extract data from the datasheet}
Arecordset: = Createoleobject (ADODB. RecordSet);
Arecordset.open (wfjcommu,aconnection,
adopenstatic,adlockoptimistic,adcmdtable);
memo1.lines.clear;
Memo1.lines.add (the original contents of the ******** datasheet are as follows ********);
{Displays the domain name of each domain}
strtemp: =;
for Intindex: = 0 to Arecordset.fields.count-1 do
strtemp: = strtemp + arecordset.fields[intindex].name+;;
Memo1.lines.add (strtemp);
{Displays the contents of each field}
while does arecordset.eof do
begin
strtemp: =;
for Intindex: = 0 to Arecordset.fields.count-1 do
strtemp: = strtemp + arecordset.fields
[intindex].value+;;
Memo1.lines.add (strtemp);
arecordset.movenext;//move to the next bar, next
end;
{Add a record}
arecordset.addnew;//increased, Append
Arecordset.fields[aname]: = 1;
//Access
in a fieldbyname manner
arecordset.fields[portable]: = 2;
Arecordset.fields (2): = 3;
//Access
in Fields[index] mode
Arecordset.fields (3): = 4;
Arecordset.fields (4): = 5;
arecordset.update;//Update, Post
arecordset.movefirst;//move to the first one,
Memo1.lines.add (******** adds a recorded data table after the contents of the following ********);
{Displays the contents of each field}
while does arecordset.eof do
begin
strtemp: =;
for Intindex: = 0 to Arecordset.
fields.count-1 do
strtemp: = strtemp +
arecordset.fields[intindex].value+;;
Memo1.lines.add (strtemp);
arecordset.movenext;//move to the next bar, next
end;
{Modify last record}
Arecordset.movelast;
Arecordset.fields[aname]: = 11;
//Access
in a fieldbyname manner
Arecordset.fields[portable]: = 22;
Arecordset.fields (2): = 33;
//Access
in Fields[index] mode
Arecordset.fields (3): = 44;
Arecordset.fields (4): = 55;
arecordset.update;//Update, Post
arecordset.movefirst;//move to the first one,
Memo1.lines.add (******** modified the last record of the datasheet after the contents of the table are as follows ********);
{Displays the contents of each field}
while does arecordset.eof do
begin
strtemp: =;
for Intindex: = 0 to
arecordset.fields.count-1 do
strtemp: = strtemp +
arecordset.fields[intindex].value+;;
Memo1.lines.add (strtemp);
arecordset.movenext;//move to the next bar, next
end;
{Delete last record}
arecordset.movelast;//move to the end, last
arecordset.delete;//deleted, delete
arecordset.update;//Update, in Delphi do not need
arecordset.movefirst;//move to the first one,
Memo1.lines.add (******** deleted the last record of the datasheet after the contents of the table are as follows ********);
{Displays the contents of each field}
while does arecordset.eof do
begin
strtemp: =;
for Intindex: = 0 to Arecordset.fields.count-1 do
strtemp: = strtemp + Arecordset.
fields[intindex].value+;;
Memo1.lines.add (strtemp);
arecordset.movenext;//move to the next bar, next
end;
Arecordset.close; {Close Data Set}
{Query with SQL statements, query for records with the name "John"}
{Note that in SQL statements, strings should be included in single quotes}
arecordset.open (SELECT * FROM Wfjcommu
where aname = John,
Aconnection,adopenstatic,adlockoptimistic,
adCmdText);
The contents of
Memo1.lines.add (******** John are as follows ********);
memo1.lines.add (Total + inttostr (arecordset.recordcount) + stripe matching record);
{Show contents of individual fields}
while does arecordset.eof do
begin
strtemp: =;
for Intindex: = 0 to Arecordset.fields.count-1 do
strtemp: = strtemp + arecordset.fields
[intindex].value+;;
Memo1.lines.add (strtemp);
arecordset.movenext;//move to the next bar, next
end;
{Turn off Datasets and database connections}
Arecordset.close;
Aconnection.close;
end;
The above program is debugged by PWIN98+DELPHI3.0+PWS (Personal Web Server) 4.0. For more information about ADO objects, see the documentation for the ASP Help file or InterDev Help file or OFFICE2000.