选择显示字体大小

在dephi程序中使用ado对象存取odbc数续


  p>3.其它常见对象(与delphi对应的对象):

adodb.field:tfield adodb.parameter:
   tpara adodb.error:edbengineerror
adodb.command:无 adodb.property:无

  下面来看一个应用例子,听别人说总不如自己看实际的例子来体会。在这个例子中,将演示如何利用ado对象来对一个数据表进行查询、增加记录、修改记录和删除记录操作。具体的用法请参见程序中的注释,如果有点delphi数据库编程经验,相信不难理解。
  在我们的例子使用的数据库为test.mdb,其中有一个数据表为wfjcommu,有五个字段aname、portable、tel、bp、postaddress,分别表示姓名、手机号、电话号码、呼机号码和通信地址。
procedure tform1.button1click(sender: tobject);
{*****************************************************
  用ado操作odbc数据库本程序中,将创建一个临时的odbc系统数据源,指向一个msaccess数据库,然后对其中的数据表进行显示、增加、修改、删除和查询操作注意:请在uses语句中包含comobj单元
*****************************************************}

const{ 一些常量声明,详细请参见adovbs.inc }
{ ---- commandtype的常量说明 ---- }

adcmdunknown = 0008;//未知,
需要系统来判断,速度慢,为缺省值
adcmdtext = 0001;//命令语句如sql语句
adcmdtable = 0002;//数据表名称
adcmdstoredproc = 0004;//存储过程名称

{ ---- cursortype的常量说明 ---- }

adopenforwardonly = 0;//只能由前向后单向访问,为缺省值
adopenkeyset = 1;//可见其他用户对数据的修改,
但对其它用户的增加和删除不可见
adopendynamic = 2;//其他用户对数据的增加修改和删除均可见
adopenstatic = 3;//其他用户对数据的增加修改和删除均不可见

{---- locktype的常量说明 ---}

adlockreadonly = 1;//只读,为缺省值
adlockpessimistic = 2;//在修改时,按单个记录锁定
adlockoptimistic = 3;//在修改后更新时,按单个记录锁定
adlockbatchoptimistic = 4;//在成批更新时记录锁定
var
aconnection, arecordset variant;
longinttemp : integer;
strtemp : string;
intindex : integer;
begin

{创建一个临时的odbc数据源,向一个msaccess数据库,利用此dsn建立一个数据库连接}

aconnection := createoleobject(adodb.connection);
aconnection.open(driver={microsoft access driver
(*.mdb)};dbq=c:\.netpub\wwwroot\test);

{建立一个数据集对象,并从数据表中提取数据}

arecordset := createoleobject(adodb.recordset);
arecordset.open( wfjcommu,aconnection,
adopenstatic,adlockoptimistic,adcmdtable );
memo1.lines.clear;
memo1.lines.add(********数据表原有的内容如下********);

{显示各个域的域名}

strtemp := ;
for intindex := 0 to arecordset.fields.count - 1 do
strtemp := strtemp + arecordset.fields[intindex].name+;;
memo1.lines.add( strtemp );

{显示各个域的内容}

while not 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;//移到下条,next
end;

{增加一个记录}

arecordset.addnew;//增加,append
arecordset.fields[aname] := 1;
//以fieldbyname的方式存取
arecordset.fields[portable] := 2;
arecordset.fields(2) := 3;
//以fields[index]的方式存取
arecordset.fields(3) := 4;
arecordset.fields(4) := 5;
arecordset.update;//更新,post
arecordset.movefirst;//移到首条,first
memo1.lines.add(********增加了一条记录后的数据表的内容如下********);

{显示各个域的内容}

while not 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;//移到下条,next
end;

{修改最后一条记录}

arecordset.movelast;
arecordset.fields[aname] := 11;
//以fieldbyname的方式存取
arecordset.fields[portable] := 22;
arecordset.fields(2) := 33;
//以fields[index]的方式存取
arecordset.fields(3) := 44;
arecordset.fields(4) := 55;
arecordset.update;//更新,post
arecordset.movefirst;//移到首条,first
memo1.lines.add(********修改了最后一条记录后的数据表的内容如下********);

{显示各个域的内容}

while not 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;//移到下条,next
end;

{删除最后一条记录}

arecordset.movelast;//移到末条,last
arecordset.delete;//删除,delete
arecordset.update;//更新,在delphi不需要
arecordset.movefirst;//移到首条,first
memo1.lines.add(********删除了最后一条记录后的数据表的内容如下********);

{显示各个域的内容}

while not 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;//移到下条,next
end;

arecordset.close;{关闭数据集}
{用sql语句进行查询,查询姓名为“张三”的记录}
{注意,在sql语句中,字符串应该用单引号包括起来}

arecordset.open( select * from wfjcommu
where aname = 张三,
aconnection,adopenstatic,adlockoptimistic,
adcmdtext );
memo1.lines.add(********张三的内容如下********);
memo1.lines.add( 共有 + inttostr( arecordset.recordcount ) + 条匹配的记录 );

{显示各个域的内容}

while not 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;//移到下条,next
end;

{关闭数据集和数据库连接}

arecordset.close;
aconnection.close;
end;

  以上程序在pwin98+delphi3.0+pws(personal web server)4.0下调试通过.关于ado对象的详细资料,请参见asp帮助文件或interdev帮助文件或office2000的有关文档.
 


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons