function CompactDatabase(AFileName,APassWord:string):boolean
//压缩与修复数据库,覆盖源文件
const
SConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0Data Source=%s'
+'Jet OLEDB:Database Password=%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对象超过作用域自动释放
// JE.closedatabase(SConnectionString)
OleCheck(JE.CompactDatabase(format(SConnectionString,[AFileName,APassWord]),
format(SConnectionString,[STempFileName,APassWord])))//压缩数据库
//复制并覆盖源数据库文件,如果复制失败则函数返回假,压缩成功但没有达到函数的功能
result:=CopyFile(PChar(STempFileName),PChar(AFileName),false)
DeleteFile(STempFileName)//删除临时文件
// JE.opendatabase(SConnectionString)
except
result:=false//压缩失败
end
end
用Sql。备份: AdoQuery1.SQL.Text := 'backup database testDFW to disk = ' + '''' + 'e:\backup.dat' + ''''
恢复: AdoQuery1.SQL.Text := 'restore database testDFW from disk =' + '''' + 'e:\backup.dat' + ''''
//下面代码针对sqlserver的 Q是AdoQuery 组件//备份
var sql:String
begin
if (filename.Text='') then
begin
Application.MessageBox('请选择文件','提示',MB_ICONWARNING)
exit
end
sql:='BACKUP DATABASE "mao" TO DISK ='''+filename.Text+''' with init'
Q.SQL.Text:=sql
q.ExecSQL
Application.MessageBox('数据库备份成功','提示',MB_ICONWARNING)
end
//还原
var sql:String
begin
if (fname.Text='') then
begin
Application.MessageBox('请选择文件','提示',MB_ICONWARNING)
exit
end
Q.SQL.Text:='use master'
Q.ExecSQL
sql:='RESTORE DATABASE "mao" FROM DISK='''+fname.Text+''''
Q.SQL.Text:=sql
q.ExecSQL
Q.SQL.Text:='Use mao'
Q.ExecSQL
Application.MessageBox('数据库还原成功','提示',MB_ICONWARNING)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)