| 订阅 | 在线投稿
分享
 
 
当前位置: 王朝网络 >> delphi >> Delphi中的Access技巧集 Delphi zhong de Access ji qiao ji
 

Delphi中的Access技巧集

2006-12-16 17:32:44 编辑來源:互联网 繁體版 评论
 
 
本文为【Delphi中的Access技巧集】的汉字拼音对照版显示拼音
  1.DELPHIzhongcaozuoACCESSshujuku(jianli.mdbwenjian,yasuoshujuku)

  yixiadaimazaiWIN2K,D6,MDAC2.6xiaceshitongguo,

  bianyihaodechengxuzaiWIN98dierbanwuACCESShuanjingxiayunxingchenggong.

  //zaizhiqianuses ComObj,ActiveX

  //shengminglianjiezifuchuan

  Const

  SConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;'

  +'Jet OLEDB:Database Password=%s;';

  

  //=============================================================================

  // Procedure: GetTempPathFileName

  // Author : ysai

  // Date : 2003-01-27

  // Arguments: (None)

  // Result : string

  //=============================================================================

  function GetTempPathFileName():string;

  //qudelinshiwenjianming

  var

  SPath,Sfile&:array [0..254] of char;

  begin

  GetTempPath(254,SPath);

  GetTempFileName(SPath,'~SM',0,SFile);

  result:=SFile;

  DeleteFile(PChar(result));

  end;

  //=============================================================================

  // Procedure: CreateAccessFile

  // Author : ysai

  // Date : 2003-01-27

  // Arguments: FileName:String;PassWord:string=''

  // Result : boolean

  //=============================================================================

  function CreateAccessFile(FileName:String;PassWord:string=''):boolean;

  //jianliAccesswenjianruguowenjiancunzaizeshibai

  var

  STempFileName:string;

  vCatalog:OleVariant;

  begin

  STempFileName:=GetTempPathFileName;

  try

  vCatalog:=CreateOleObject('ADOX.Catalog');

  vCatalog.Create(format(SConnectionString,[STempFileName,PassWord]));

  result:=CopyFile(PChar(STempFileName),PChar(FileName),True);

  DeleteFile(STempFileName);

  except

  result:=false;

  end;

  end;

  //=============================================================================

  // Procedure: CompactDatabase

  // Author : ysai

  // Date : 2003-01-27

  // Arguments: AFileName,APassWord:string

  // Result : boolean

  //=============================================================================

  function CompactDatabase(AFileName,APassWord:string):boolean;

  //yasuoyuxiufushujuku,fugaiyuanwenjian

  var

  STempFileName:string;

  vJE:OleVariant;

  begin

  STempFileName:=GetTempPathFileName;

  try

  vJE:=CreateOleObject('JRO.JetEngine');

  vJE.CompactDatabase(format(SConnectionString,[AFileName,APassWord]),

  format(SConnectionString,[STempFileName,APassWord]));

  result:=CopyFile(PChar(STempFileName),PChar(AFileName),false);

  DeleteFile(STempFileName);

  except

  result:=false;

  end;

  end;

   //=============================================================================

  // Procedure: ChangeDatabasePassword

  // Author : ysai

  // Date : 2003-01-27

  // Arguments: AFileName,AOldPassWord,ANewPassWord:string

  // Result : boolean

  //=============================================================================

  function ChangeDatabasePassword(AFileName,AOldPassWord,ANewPassWord:string):boolean;

  //xiugaiACCESSshujukumima

  var

  STempFileName:string;

  vJE:OleVariant;

  begin

  STempFileName:=GetTempPathFileName;

  try

  vJE:=CreateOleObject('JRO.JetEngine');

  vJE.CompactDatabase(format(SConnectionString,[AFileName,AOldPassWord]),

  format(SConnectionString,[STempFileName,ANewPassWord]));

  result:=CopyFile(PChar(STempFileName),PChar(AFileName),false);

  DeleteFile(STempFileName);

  except

  result:=false;

  end;

  end;

  2.ACCESSzhong使shiyongSQLyujuyingzhuyidedifangjijidianjiqiao

  yixiaSQLyujuzaiACCESS XPdechaxunzhongceshitongguo

  jianbiao:

  Create Table Tab1 (

  ID Counter,

  Name string,

  Age integer,

  [Date] DateTime);

  jiqiao:

  zizengziduanyong Counter shengming.

  ziduanmingweiguanjianzideziduanyongfangkuohao[]kuoqilai,shuzizuoweiziduanmingyekexing.

  jianlisuoyin:

  xiamiandeyujuzaiTab1deDatelieshangjianlikechongfusuoyin

  Create Index iDate ON Tab1 ([Date]);

  wanchenghouACCESSzhongziduanDatesuoyinshuxingxianshiwei - you(youchongfu).

  xiamiandeyujuzaiTab1deNamelieshangjianlibukechongfusuoyin

  Create Unique Index iName ON Tab1 (Name);

  wanchenghouACCESSzhongziduanNamesuoyinshuxingxianshiwei - you(wuchongfu).

  xiamiandeyujushanchugangcaijianlidelianggesuoyin

  Drop Index iDate ON Tab1;

  Drop Index iName ON Tab1;

  ACCESSyuSQLSERVERzhongdeUPDATEyujuduibi:

  SQLSERVERzhonggengxinduobiaodeUPDATEyuju:

  UPDATE Tab1

  SET a.Name = b.Name

  FROM Tab1 a,Tab2 b

  WHERE a.ID = b.ID;

  tongyanggongnengdeSQLyujuzaiACCESSzhongyinggaishi

  UPDATE Tab1 a,Tab2 b

  SET a.Name = b.Name

  WHERE a.ID = b.ID;

  ji:ACCESSzhongdeUPDATEyujumeiyouFROMziju,suoyouyinyongdebiaodouliezaiUPDATEguanjianzihou.

  shanglizhongruguoTab2keyibushiyigebiao,ershiyigechaxun,li:

  UPDATE Tab1 a,(Select ID,Name From Tab2) b

  SET a.Name = b.Name

  WHERE a.ID = b.ID;

  访fangwenduogebutongdeACCESSshujuku-zaiSQLzhong使shiyongInziju:

  Select a.*,b.* From Tab1 a,Tab2 b In 'db2.mdb' Where a.ID=b.ID;

  shangmiandeSQLyujuchaxunchudangqianshujukuzhongTab1hedb2.mdb(dangqianwenjianjiazhong)zhongTab2yiIDweiguanliandesuoyoujilu.

  quedian-waibushujukubunengdaimima.

  buchong:kandaougvanxkzaiyitiezhongdedafu,keyiyong

  Select * from [c:\aa\a.mdb;pwd=1111].table1;

  ACCESS XPceshitongguo

  zaiACCESSzhong访fangwenqitaODBCshujuyuan

  xializaiACCESSzhongchaxunSQLSERVERzhongdeshuju

  SELECT * FROM Tab1 IN [ODBC]

  [ODBC;Driver=SQL Server;UID=sa;PWD=;Server=127.0.0.1;DataBase=Demo;]

  waibushujuyuanlianjieshuxingdewanzhengshenshushi:

  [ODBC;DRIVER=driver;SERVER=server;DATABASE=database;UID=user;PWD=password;]

  qizhongdeDRIVER=driverkeyizaizhucebiaozhongde

  HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\

  zhongzhaodao

  yigoushujukuzhijiandaoshujushenjian bixuejian

  ACCESSzhichizichaxun

  ACCESSzhichiwailianjie,danbubaokuowanzhengwaibulianjie,ruzhichi

  LEFT JOIN huo RIGHT JOIN

  danbuzhichi

  FULL OUTER JOIN huo FULL JOIN

  ACCESSzhongderiqichaxun

  zhuyi:ACCESSzhongderiqishijianfengefushi#erbushiyinhao

  Select * From Tab1 Where [Date]>#2002-1-1#;

  zaiDELPHIzhongwozheyangyong

  SQL.Add(Format(

  'Select * From Tab1 Where [Date]>#%s#;',

  [DateToStr(Date)]));

  ACCESSzhongdezifuchuankeyiyongshuangyinhaofenge,danSQLSERVERburen,suoyiweileqianyifang便bianhejianrong, jianyiyongdanyinhaozuoweizifuchuanfengefu.原文】【拼音
 
 
 
 
上一篇《WHAT"S NEW IN DELPHI 6》
下一篇《为Delphi程序添加自动滚动的工具栏》
 
 
 
 
 
 
日版宠物情人插曲《Winding Road》歌词

日版宠物情人2017的插曲,很带节奏感,日语的,女生唱的。 最后听见是在第8集的时候女主手割伤了,然后男主用嘴帮她吸了一下,插曲就出来了。 歌手:Def...

兄弟共妻,我成了他们夜里的美食

老钟家的两个儿子很特别,就是跟其他的人不太一样,魔一般的执着。兄弟俩都到了要结婚的年龄了,不管自家老爹怎么磨破嘴皮子,兄弟俩说不娶就不娶,老父母为兄弟两操碎了心...

如何磨出破洞牛仔裤?牛仔裤怎么剪破洞?

把牛仔裤磨出有线的破洞 1、具体工具就是磨脚石,下面垫一个硬物,然后用磨脚石一直磨一直磨,到把那块磨薄了,用手撕开就好了。出来的洞啊很自然的。需要猫须的话调几...

我就是扫描下图得到了敬业福和爱国福

先来看下敬业福和爱国福 今年春节,支付宝再次推出了“五福红包”活动,表示要“把欠大家的敬业福都还给大家”。 今天该活动正式启动,和去年一样,需要收集“五福”...

冰箱异味产生的原因和臭味去除的方法

有时候我们打开冰箱就会闻到一股异味,冰箱里的这种异味是因为一些物质发出的气味的混合体,闻起来让人恶心。 产生这些异味的主要原因有以下几点。 1、很多人有这种习...

 
 
1.DELPHI中操作ACCESS数据库(建立.mdb文件,压缩数据库) 以下代码在WIN2K,D6,MDAC2.6下测试通过, 编译好的程序在WIN98第二版无ACCESS环境下运行成功. //在之前uses ComObj,ActiveX //声明连接字符串 Const SConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;' +'Jet OLEDB:Database Password=%s;'; //============================================================================= // Procedure: GetTempPathFileName // Author : ysai // Date : 2003-01-27 // Arguments: (None) // Result : string //============================================================================= function GetTempPathFileName():string; //取得临时文件名 var SPath,Sfile&:array [0..254] of char; begin GetTempPath(254,SPath); GetTempFileName(SPath,'~SM',0,SFile); result:=SFile; DeleteFile(PChar(result)); end; //============================================================================= // Procedure: CreateAccessFile // Author : ysai // Date : 2003-01-27 // Arguments: FileName:String;PassWord:string='' // Result : boolean //============================================================================= function CreateAccessFile(FileName:String;PassWord:string=''):boolean; //建立Access文件,如果文件存在则失败 var STempFileName:string; vCatalog:OleVariant; begin STempFileName:=GetTempPathFileName; try vCatalog:=CreateOleObject('ADOX.Catalog'); vCatalog.Create(format(SConnectionString,[STempFileName,PassWord])); result:=CopyFile(PChar(STempFileName),PChar(FileName),True); DeleteFile(STempFileName); except result:=false; end; end; //============================================================================= // Procedure: CompactDatabase // Author : ysai // Date : 2003-01-27 // Arguments: AFileName,APassWord:string // Result : boolean //============================================================================= function CompactDatabase(AFileName,APassWord:string):boolean; //压缩与修复数据库,覆盖源文件 var STempFileName:string; vJE:OleVariant; begin STempFileName:=GetTempPathFileName; try vJE:=CreateOleObject('JRO.JetEngine'); vJE.CompactDatabase(format(SConnectionString,[AFileName,APassWord]), format(SConnectionString,[STempFileName,APassWord])); result:=CopyFile(PChar(STempFileName),PChar(AFileName),false); DeleteFile(STempFileName); except result:=false; end; end; //============================================================================= // Procedure: ChangeDatabasePassword // Author : ysai // Date : 2003-01-27 // Arguments: AFileName,AOldPassWord,ANewPassWord:string // Result : boolean //============================================================================= function ChangeDatabasePassword(AFileName,AOldPassWord,ANewPassWord:string):boolean; //修改ACCESS数据库密码 var STempFileName:string; vJE:OleVariant; begin STempFileName:=GetTempPathFileName; try vJE:=CreateOleObject('JRO.JetEngine'); vJE.CompactDatabase(format(SConnectionString,[AFileName,AOldPassWord]), format(SConnectionString,[STempFileName,ANewPassWord])); result:=CopyFile(PChar(STempFileName),PChar(AFileName),false); DeleteFile(STempFileName); except result:=false; end; end; 2.ACCESS中使用SQL语句应注意的地方及几点技巧 以下SQL语句在ACCESS XP的查询中测试通过 建表: Create Table Tab1 ( ID Counter, Name string, Age integer, [Date] DateTime); 技巧: 自增字段用 Counter 声明. 字段名为关键字的字段用方括号[]括起来,数字作为字段名也可行. 建立索引: 下面的语句在Tab1的Date列上建立可重复索引 Create Index iDate ON Tab1 ([Date]); 完成后ACCESS中字段Date索引属性显示为 - 有(有重复). 下面的语句在Tab1的Name列上建立不可重复索引 Create Unique Index iName ON Tab1 (Name); 完成后ACCESS中字段Name索引属性显示为 - 有(无重复). 下面的语句删除刚才建立的两个索引 Drop Index iDate ON Tab1; Drop Index iName ON Tab1; ACCESS与SQLSERVER中的UPDATE语句对比: SQLSERVER中更新多表的UPDATE语句: UPDATE Tab1 SET a.Name = b.Name FROM Tab1 a,Tab2 b WHERE a.ID = b.ID; 同样功能的SQL语句在ACCESS中应该是 UPDATE Tab1 a,Tab2 b SET a.Name = b.Name WHERE a.ID = b.ID; 即:ACCESS中的UPDATE语句没有FROM子句,所有引用的表都列在UPDATE关键字后. 上例中如果Tab2可以不是一个表,而是一个查询,例: UPDATE Tab1 a,(Select ID,Name From Tab2) b SET a.Name = b.Name WHERE a.ID = b.ID; 访问多个不同的ACCESS数据库-在SQL中使用In子句: Select a.*,b.* From Tab1 a,Tab2 b In 'db2.mdb' Where a.ID=b.ID; 上面的SQL语句查询出当前数据库中Tab1和db2.mdb(当前文件夹中)中Tab2以ID为关联的所有记录. 缺点-外部数据库不能带密码. 补充:看到ugvanxk在一贴中的答复,可以用 Select * from [c:\aa\a.mdb;pwd=1111].table1; ACCESS XP测试通过 在ACCESS中访问其它ODBC数据源 下例在ACCESS中查询SQLSERVER中的数据 SELECT * FROM Tab1 IN [ODBC] [ODBC;Driver=SQL Server;UID=sa;PWD=;Server=127.0.0.1;DataBase=Demo;] 外部数据源连接属性的完整参数是: [ODBC;DRIVER=driver;SERVER=server;DATABASE=database;UID=user;PWD=password;] 其中的DRIVER=driver可以在注册表中的 HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ 中找到 异构数据库之间导数据参见 碧血剑 ACCESS支持子查询 ACCESS支持外连接,但不包括完整外部联接,如支持 LEFT JOIN 或 RIGHT JOIN 但不支持 FULL OUTER JOIN 或 FULL JOIN ACCESS中的日期查询 注意:ACCESS中的日期时间分隔符是#而不是引号 Select * From Tab1 Where [Date]>#2002-1-1#; 在DELPHI中我这样用 SQL.Add(Format( 'Select * From Tab1 Where [Date]>#%s#;', [DateToStr(Date)])); ACCESS中的字符串可以用双引号分隔,但SQLSERVER不认,所以为了迁移方便和兼容, 建议用单引号作为字符串分隔符.
󰈣󰈤
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
时尚秀气模特裴裴(8)
时尚秀气模特裴裴(7)
时尚秀气模特裴裴(6)
时尚秀气模特裴裴(5)
白墙黑瓦
广州东郊[原创]
八美至丹巴的东谷天然盆景
不一样的
 
>>返回首页<<
 
 
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 热帖排行
 
 
 
 
©2005- 王朝网络 版权所有