在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
以下代码在WIN2K,D6,MDAC2.6下测试通过, 编译好的程序在WIN98第二版无ACCESS环境下运行成功.
1//在之前uses ComObj,ActiveX
2//声明连接字符串 3Const 4SConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;' 5+'Jet OLEDB:Database Password=%s;'; 6 7 8//============================================================================= 9// Procedure: GetTempPathFileName 10// Author : ysai 11// Date : 2003-01-27 12// Arguments: (None) 13// Result : string 14//============================================================================= 15function GetTempPathFileName():string; 16//取得临时文件名 17var 18SPath,Sfile&:array [0..254] of char; 19begin 20GetTempPath(254,SPath); 21GetTempFileName(SPath,'~SM',0,SFile); 22result:=SFile; 23DeleteFile(PChar(result)); 24end; 25 26//============================================================================= 27// Procedure: CreateAccessFile 28// Author : ysai 29// Date : 2003-01-27 30// Arguments: FileName:String;PassWord:string='' 31// Result : boolean 32//============================================================================= 33function CreateAccessFile(FileName:String;PassWord:string=''):boolean; 34//建立Access文件,如果文件存在则失败 35var 36STempFileName:string; 37vCatalog:OleVariant; 38begin 39STempFileName:=GetTempPathFileName; 40try 41vCatalog:=CreateOleObject('ADOX.Catalog'); 42vCatalog.Create(format(SConnectionString,[STempFileName,PassWord])); 43result:=CopyFile(PChar(STempFileName),PChar(FileName),True); 44DeleteFile(STempFileName); 45except 46result:=false; 47end; 48end; 49 50//============================================================================= 51// Procedure: CompactDatabase 52// Author : ysai 53// Date : 2003-01-27 54// Arguments: AFileName,APassWord:string 55// Result : boolean 56//============================================================================= 57function CompactDatabase(AFileName,APassWord:string):boolean; 58//压缩与修复数据库,覆盖源文件 59var 60STempFileName:string; 61vJE:OleVariant; 62begin 63STempFileName:=GetTempPathFileName; 64try 65vJE:=CreateOleObject('JRO.JetEngine'); 66vJE.CompactDatabase(format(SConnectionString,[AFileName,APassWord]), 67format(SConnectionString,[STempFileName,APassWord])); 68result:=CopyFile(PChar(STempFileName),PChar(AFileName),false); 69DeleteFile(STempFileName); 70except 71result:=false; 72end; 73end; 74 75 //============================================================================= 76// Procedure: ChangeDatabasePassword 77// Author : ysai 78// Date : 2003-01-27 79// Arguments: AFileName,AOldPassWord,ANewPassWord:string 80// Result : boolean 81//============================================================================= 82function ChangeDatabasePassword(AFileName,AOldPassWord,ANewPassWord:string):boolean; 83//修改ACCESS数据库密码 84var 85STempFileName:string; 86vJE:OleVariant; 87begin 88STempFileName:=GetTempPathFileName; 89try 90vJE:=CreateOleObject('JRO.JetEngine'); 91vJE.CompactDatabase(format(SConnectionString,[AFileName,AOldPassWord]), 92format(SConnectionString,[STempFileName,ANewPassWord])); 93result:=CopyFile(PChar(STempFileName),PChar(AFileName),false); 94DeleteFile(STempFileName); 95except 96result:=false; 97end; 98end; 99 100 2.ACCESS中使用SQL语句应注意的地方及几点技巧 建立索引: ACCESS与SQLSERVER中的UPDATE语句对比: UPDATE Tab1 SET a.Name = b.Name FROM Tab1 a,Tab2 b WHERE a.ID = b.ID;
UPDATE Tab1 a,Tab2 b
即:ACCESS中的UPDATE语句没有FROM子句,所有引用的表都列在UPDATE关键字后. SET a.Name = b.Name WHERE a.ID = b.ID; 上例中如果Tab2可以不是一个表,而是一个查询,例: UPDATE Tab1 a,(Select ID,Name From Tab2) b SET a.Name = b.Name WHERE a.ID = b.ID;
访问多个不同的ACCESS数据库-在SQL中使用In子句: 在ACCESS中访问其它ODBC数据源
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支持外连接,但不包括完整外部联接,如支持 ACCESS中的日期查询 ACCESS中的字符串可以用双引号分隔,但SQLSERVER不认,所以为了迁移方便和兼容, 建议用单引号作为字符串分隔符. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论