powerbuilder 备份和恢复数据库


备份:

string backup
STRING pathstring 
int li_net
if GetFilesaveName("选择文件",pathstring,backup,"bak","bak文件(*.bak),*.bak") = 1 then

	if pathstring = '' then 
		messagebox("提示","请输入备份数据库名称!") 
		return 
	end if
	IF FileExists ( pathstring ) THEN  
		li_net=Messagebox('信息','提示:系统发现在"'+pathstring+'"目录下已经存在一个同名的文件.~r~n~n要替换它吗?',Question!,YesNo!) 
		IF li_net= 1 THEN   
			FileDelete ( pathstring ) 
		ELSE
			return
		END IF 
	end if
	setpointer(hourglass!)
	backup= "backup database "+sqlca.database+" to disk = " + "'" + pathstring + "'"; 
	sqlca.AutoCommit = true
	EXECUTE IMMEDIATE :backup;
	setpointer(arrow!)
	if sqlca.sqlcode = 0 then 
		messagebox('提示信息','数据库备份成功!') 
	else 
		messagebox("错误信息",sqlca.sqlerrtext) 
	end if
	SQLCA.AutoCommit = false 
end if


恢复:


string backup
STRING pathstring 
if GetFileopenName("选择文件",pathstring,backup,"bak","bak文件(*.bak),*.bak") = 1 then

	if pathstring = '' then 
		messagebox("提示","请输入数据库名称!") 
		return 
	end if
	sqlca.AutoCommit = true
	backup="ALTER DATABASE "+sqlca.database+" SET OFFLINE WITH ROLLBACK IMMEDIATE";
	EXECUTE IMMEDIATE :backup;
	backup= "use master restore database "+sqlca.database+" from disk='" +pathstring+ "' with replace"; 
	
	EXECUTE IMMEDIATE :backup; 
	if sqlca.sqlcode = 0 then 
		messagebox('提示信息','数据库恢复成功!') 
	else 
		messagebox("系统信息","出错了+"+sqlca.sqlerrtext) 
	end if
	sqlca.autocommit=false

	
	backup="ALTER  database “+sqlca.database+" set   online  ";
	EXECUTE IMMEDIATE :backup;

	disconnect;
	connect;	//需重新连接一次,否则恢复后无法正常读取数据库内容
end if






你可能感兴趣的:(powerbuilder 备份和恢复数据库)