Delphi测试数据库连接时间

Delphi单元PAS代码:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Phys.MSSQL,
  FireDAC.Phys.MSSQLDef, FireDAC.VCLUI.Wait, FireDAC.Comp.UI, Data.DB,
  FireDAC.Comp.Client, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    FDConnection1: TFDConnection;
    FDGUIxWaitCursor1: TFDGUIxWaitCursor;
    grp1: TGroupBox;
    edtServer: TEdit;
    lbl1: TLabel;
    edtUser_Name: TEdit;
    lbl2: TLabel;
    edtPassword: TEdit;
    lbl3: TLabel;
    grp2: TGroupBox;
    edtCount: TEdit;
    lbl4: TLabel;
    btn1: TButton;
    mmo1: TMemo;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
  t1, t2, t3: Extended;
  i, count: integer;
begin
  FDConnection1.Close;
  FDConnection1.Params.Values['Server'] := edtServer.Text;
  FDConnection1.Params.Values['User_Name'] := edtUser_Name.Text;
  FDConnection1.Params.Values['Password'] := edtPassword.Text;

  t1 := GetTickCount;
  count := StrToIntDef(edtCount.Text, 0);
  for i := 0 to count - 1 do
  begin
    FDConnection1.Open();
    FDConnection1.Close;
  end;
  t2 := GetTickCount;
  t3 := (t2 - t1) / 1000;
  mmo1.Lines.Add(format('测试次数:%d 次;耗时:%f 秒;平均每次:%f 秒;', [count, t3, t3 / count]
    )); // 显示耗时
end;

end.


Delphi窗体DFM代码:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 520
  ClientWidth = 560
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  DesignSize = (
    560
    520)
  PixelsPerInch = 96
  TextHeight = 13
  object grp1: TGroupBox
    Left = 20
    Top = 24
    Width = 328
    Height = 145
    Caption = #25968#25454#24211
    TabOrder = 0
    object lbl1: TLabel
      Left = 60
      Top = 24
      Width = 72
      Height = 13
      Caption = #26381#21153#22120#22320#22336#65306
      FocusControl = edtServer
    end
    object lbl2: TLabel
      Left = 84
      Top = 64
      Width = 48
      Height = 13
      Caption = #29992#25143#21517#65306
      FocusControl = edtUser_Name
    end
    object lbl3: TLabel
      Left = 96
      Top = 104
      Width = 36
      Height = 13
      Caption = #23494#30721#65306
      FocusControl = edtPassword
    end
    object edtServer: TEdit
      Left = 144
      Top = 21
      Width = 121
      Height = 21
      TabOrder = 0
    end
    object edtUser_Name: TEdit
      Left = 144
      Top = 61
      Width = 121
      Height = 21
      TabOrder = 1
    end
    object edtPassword: TEdit
      Left = 144
      Top = 101
      Width = 121
      Height = 21
      TabOrder = 2
    end
  end
  object grp2: TGroupBox
    Left = 20
    Top = 192
    Width = 517
    Height = 305
    Anchors = [akLeft, akTop, akRight, akBottom]
    Caption = #27979#35797
    TabOrder = 1
    DesignSize = (
      517
      305)
    object lbl4: TLabel
      Left = 35
      Top = 27
      Width = 60
      Height = 13
      FocusControl = edtCount
    end
    object edtCount: TEdit
      Left = 101
      Top = 24
      Width = 121
      Height = 21
      TabOrder = 1
      Text = '1000'
    end
    object btn1: TButton
      Left = 234
      Top = 22
      Width = 75
      Height = 25
      Caption = #24320#22987#27979#35797
      TabOrder = 0
      OnClick = btn1Click
    end
    object mmo1: TMemo
      Left = 15
      Top = 64
      Width = 482
      Height = 225
      Anchors = [akLeft, akTop, akRight, akBottom]
      ScrollBars = ssBoth
      TabOrder = 2
    end
  end
  object FDConnection1: TFDConnection
    Params.Strings = (
      'User_Name=sa'
      'Password=123'
      'Server=127.0.0.1'
      'DriverID=MSSQL')
    LoginPrompt = False
    Left = 420
    Top = 16
  end
  object FDGUIxWaitCursor1: TFDGUIxWaitCursor
    Provider = 'Forms'
    Left = 420
    Top = 72
  end
end



上一篇: Delphi的IDHTTP的基本用法
下一篇: Delphi FormatFloat 函数
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: Delphi 测试 数据库 连接 时间
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭