8 个解决方案
#1
>>在access中可以手工解决这一问题
????
運行一段時間, 就壓縮一次數據庫了
????
運行一段時間, 就壓縮一次數據庫了
#2
在access中是可以手工解决,但在程序中如何实现呢?运行一段时间后也不会自己压缩阿!
#3
运行一段时间,压缩一下,或退出时候自动压缩。
function TForm1.CompactDatabase(AFileName: string): boolean;
const
SConnectionString =
'Provider=Microsoft.Jet.OLEDB.4.0;Password=XXXXXX;' +
'User ID=XXXXXX;' +
'Data Source=%S;'
var
SPath, SFile: array[0..254] of Char;
STempFileName: string;
JE: OleVariant;
begin
GetTempPath(40, SPath); //取得Windows的Temp路径
GetTempFileName(SPath, '~CP', 0, SFile); //取得Temp文件名,Windows将自动建立0字节文件
STempFileName := SFile; //PChar->String
DeleteFile(STempFileName); //删除Windows建立的0字节文件
try
JE := CreateOleObject('JRO.JetEngine'); //建立OLE对象,函数结束OLE对象超过作用域自动释放
OleCheck(JE.CompactDatabase(Format(SConnectionString, [AFileName1]), format(SConnectionString, [STempFileName, AFileName1]))); //压缩数据库
//复制并覆盖源数据库文件,如果复制失败则函数返回假,压缩成功但没有达到函数的功能
result := CopyFile(PChar(STempFileName), PChar(AFileName), false);
DeleteFile(STempFileName); //删除临时文件
except
result := false; //压缩失败
end;
end;
function TForm1.CompactDatabase(AFileName: string): boolean;
const
SConnectionString =
'Provider=Microsoft.Jet.OLEDB.4.0;Password=XXXXXX;' +
'User ID=XXXXXX;' +
'Data Source=%S;'
var
SPath, SFile: array[0..254] of Char;
STempFileName: string;
JE: OleVariant;
begin
GetTempPath(40, SPath); //取得Windows的Temp路径
GetTempFileName(SPath, '~CP', 0, SFile); //取得Temp文件名,Windows将自动建立0字节文件
STempFileName := SFile; //PChar->String
DeleteFile(STempFileName); //删除Windows建立的0字节文件
try
JE := CreateOleObject('JRO.JetEngine'); //建立OLE对象,函数结束OLE对象超过作用域自动释放
OleCheck(JE.CompactDatabase(Format(SConnectionString, [AFileName1]), format(SConnectionString, [STempFileName, AFileName1]))); //压缩数据库
//复制并覆盖源数据库文件,如果复制失败则函数返回假,压缩成功但没有达到函数的功能
result := CopyFile(PChar(STempFileName), PChar(AFileName), false);
DeleteFile(STempFileName); //删除临时文件
except
result := false; //压缩失败
end;
end;
#4
AFileName是要压缩的数据库文件名
#5
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;
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;
#6
测试通过
use Variants,ComObj;
...
procedure TForm1.Button1Click(Sender: TObject);
var
dao:OLEVariant;
begin
adoconnection1.Close;//压缩是以独占方式进行的;压缩前要关闭数据库连接。
screen.Cursor:=crHourGlass;
dao:=CreateOleObject('DAO.DBEngine.36');//access2000要用dao3.6,在access97上用35和36都可以
dao.CompactDatabase('TestDB1.mdb','temp.mdb');
DeleteFile('TestDB1.mdb');
RenameFile('temp.mdb','TestDB1.mdb');
adoconnection1.Open;
screen.Cursor:=crDefault;
Showmessage('本地数据库压缩完成');
end;
use Variants,ComObj;
...
procedure TForm1.Button1Click(Sender: TObject);
var
dao:OLEVariant;
begin
adoconnection1.Close;//压缩是以独占方式进行的;压缩前要关闭数据库连接。
screen.Cursor:=crHourGlass;
dao:=CreateOleObject('DAO.DBEngine.36');//access2000要用dao3.6,在access97上用35和36都可以
dao.CompactDatabase('TestDB1.mdb','temp.mdb');
DeleteFile('TestDB1.mdb');
RenameFile('temp.mdb','TestDB1.mdb');
adoconnection1.Open;
screen.Cursor:=crDefault;
Showmessage('本地数据库压缩完成');
end;
#7
请问miky大侠:我在刚才运行了您给的那个函数,系统提示:Undeclared identifier'CreateOleObject'和Undeclared identifier'OleCheck'这是怎么回事呢?
#8
谢谢诸位大侠,尤其是samcrm,我的问题解决了,用的是samcrm大虾提供的程序,现在结帖!
#1
>>在access中可以手工解决这一问题
????
運行一段時間, 就壓縮一次數據庫了
????
運行一段時間, 就壓縮一次數據庫了
#2
在access中是可以手工解决,但在程序中如何实现呢?运行一段时间后也不会自己压缩阿!
#3
运行一段时间,压缩一下,或退出时候自动压缩。
function TForm1.CompactDatabase(AFileName: string): boolean;
const
SConnectionString =
'Provider=Microsoft.Jet.OLEDB.4.0;Password=XXXXXX;' +
'User ID=XXXXXX;' +
'Data Source=%S;'
var
SPath, SFile: array[0..254] of Char;
STempFileName: string;
JE: OleVariant;
begin
GetTempPath(40, SPath); //取得Windows的Temp路径
GetTempFileName(SPath, '~CP', 0, SFile); //取得Temp文件名,Windows将自动建立0字节文件
STempFileName := SFile; //PChar->String
DeleteFile(STempFileName); //删除Windows建立的0字节文件
try
JE := CreateOleObject('JRO.JetEngine'); //建立OLE对象,函数结束OLE对象超过作用域自动释放
OleCheck(JE.CompactDatabase(Format(SConnectionString, [AFileName1]), format(SConnectionString, [STempFileName, AFileName1]))); //压缩数据库
//复制并覆盖源数据库文件,如果复制失败则函数返回假,压缩成功但没有达到函数的功能
result := CopyFile(PChar(STempFileName), PChar(AFileName), false);
DeleteFile(STempFileName); //删除临时文件
except
result := false; //压缩失败
end;
end;
function TForm1.CompactDatabase(AFileName: string): boolean;
const
SConnectionString =
'Provider=Microsoft.Jet.OLEDB.4.0;Password=XXXXXX;' +
'User ID=XXXXXX;' +
'Data Source=%S;'
var
SPath, SFile: array[0..254] of Char;
STempFileName: string;
JE: OleVariant;
begin
GetTempPath(40, SPath); //取得Windows的Temp路径
GetTempFileName(SPath, '~CP', 0, SFile); //取得Temp文件名,Windows将自动建立0字节文件
STempFileName := SFile; //PChar->String
DeleteFile(STempFileName); //删除Windows建立的0字节文件
try
JE := CreateOleObject('JRO.JetEngine'); //建立OLE对象,函数结束OLE对象超过作用域自动释放
OleCheck(JE.CompactDatabase(Format(SConnectionString, [AFileName1]), format(SConnectionString, [STempFileName, AFileName1]))); //压缩数据库
//复制并覆盖源数据库文件,如果复制失败则函数返回假,压缩成功但没有达到函数的功能
result := CopyFile(PChar(STempFileName), PChar(AFileName), false);
DeleteFile(STempFileName); //删除临时文件
except
result := false; //压缩失败
end;
end;
#4
AFileName是要压缩的数据库文件名
#5
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;
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;
#6
测试通过
use Variants,ComObj;
...
procedure TForm1.Button1Click(Sender: TObject);
var
dao:OLEVariant;
begin
adoconnection1.Close;//压缩是以独占方式进行的;压缩前要关闭数据库连接。
screen.Cursor:=crHourGlass;
dao:=CreateOleObject('DAO.DBEngine.36');//access2000要用dao3.6,在access97上用35和36都可以
dao.CompactDatabase('TestDB1.mdb','temp.mdb');
DeleteFile('TestDB1.mdb');
RenameFile('temp.mdb','TestDB1.mdb');
adoconnection1.Open;
screen.Cursor:=crDefault;
Showmessage('本地数据库压缩完成');
end;
use Variants,ComObj;
...
procedure TForm1.Button1Click(Sender: TObject);
var
dao:OLEVariant;
begin
adoconnection1.Close;//压缩是以独占方式进行的;压缩前要关闭数据库连接。
screen.Cursor:=crHourGlass;
dao:=CreateOleObject('DAO.DBEngine.36');//access2000要用dao3.6,在access97上用35和36都可以
dao.CompactDatabase('TestDB1.mdb','temp.mdb');
DeleteFile('TestDB1.mdb');
RenameFile('temp.mdb','TestDB1.mdb');
adoconnection1.Open;
screen.Cursor:=crDefault;
Showmessage('本地数据库压缩完成');
end;
#7
请问miky大侠:我在刚才运行了您给的那个函数,系统提示:Undeclared identifier'CreateOleObject'和Undeclared identifier'OleCheck'这是怎么回事呢?
#8
谢谢诸位大侠,尤其是samcrm,我的问题解决了,用的是samcrm大虾提供的程序,现在结帖!