备份SQL Server数据库时使用进度条

使用SQL语句在代码中备份还原SQL Server数据库,如果数据库比较大,界面只能阻塞,等待备份还原完成。这段时间无法显示一些进度信息,让界面看起来更加友好。下面介绍一种方法,可以在备份还原的同时,显示进度条。
4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397
const
  ConnStr = 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security In' +
            'fo=False;Initial Catalog=master';

function GetProgress: Integer;
var
  Msg: WideString;
  S: string;
  Err: IErrorInfo;
  I: Integer;
begin
  // 取得OLE DB的错误或警告要通过GetErrorInfo, 即使消息不属于错误也必须通过
  // 这种方式来获取.
  if (GetErrorInfo(0, Err) = S_OK) and (Err <> nil) then
  begin
    if Err.GetDescription(Msg) = S_OK then
    begin
      S := '';
      // 取得百分比的字值
      for I := 1 to 3 do
      begin
        if Char(Msg[I]) in ['0'..'9'] then
          S := S + Msg[I]
        else
          Break;
      end;
      if TryStrToInt(S, Result) then Exit;
    end;
  end;
  Result := -1;
end;

procedure TForm1.BtnBackupClick(Sender: TObject);
const
  BackupSQL = 'BACKUP DATABASE [pubs] TO  DISK = N%s WITH NOINIT, NOUNLOAD, ' +
            'NAME = N%s, NOSKIP, STATS = 10, NOFORMAT';
  BakFile = 'D:/pubs.bak';
  BakName = 'pubs 备份';
var
  SQL: string;
  Conn: _Connection;
  Rst: _Recordset;
  RecsAffected: OleVariant;
  percent: Integer;
begin
  PBar.Position := 0;
  Conn := CoConnection.Create;
  Conn.Open(ConnStr, '', '', 0);
  SQL := Format(BackupSQL, [QuotedStr(BakFile), QuotedStr(BakName)]);
  Rst := Conn.Execute(SQL, RecsAffected, adCmdText);
  while Rst <> nil do
  begin
    percent := GetProgress;
    if percent <> -1 then
      PBar.Position := percent;
    Application.ProcessMessages;
    Rst := Rst.NextRecordset(RecsAffected);
  end;
end;

4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值