用Delphi的random函数在数据库中建表

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RzButton, DB, ADODB, StdCtrls, RzLabel, ComCtrls, ExtCtrls,DateUtils;

type
  TForm1 = class(TForm)
    DateTimePicker1: TDateTimePicker;
    DateTimePicker2: TDateTimePicker;
    RzLabel1: TRzLabel;
    RzLabel2: TRzLabel;
    ADODataSet1: TADODataSet;
    que1: TADOQuery;
    ADOConn: TADOConnection;
    RzButton1: TRzButton;
    RzLabel3: TRzLabel;
    procedure RzButton1Click(Sender: TObject);
    Function SecondToTime(I:integer):string;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Year,Month,Day : word;
  i:integer;
  j,n,k:variant;
 Num:array[ 1 ..10000000] of integer;//66043
implementation

{$R *.dfm}
Function TForm1.SecondToTime(I: integer): string; //将秒数转化成hh:mm:ss
begin
  Result := TimeToStr(I/86400);
end;
procedure TForm1.RzButton1Click(Sender: TObject);
begin


  j :=IntToStr(DaysBetWeen(DateTimePicker1.Date,DateTimePicker2.Date));//计算两日期之间相差的天数
  k :=86400*j;
  ADOConn.Close;
 // ADOConn.ConnectionString :=
 //   'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MyData;Data Source=7F119C8C20DE4DB';
  ADOConn.Open;
  ADODataSet1.Active := false;
  ADODataSet1.Active := true;

   Randomize;
   i:= 1;
  while I<k do
   begin
      RzLabel3.Caption:='正在写数,请稍等!';
      ADODataSet1.Append;
      Num[I]:=50+Random(10);
      que1.SQL.clear;
      que1.SQL.Add('insert into JSJG06(ID,Times,Data,JsjgCode)');
      que1.SQL.Add('values(:ID,:Times,:Data,:JsjgCode)');
       Year:=StrToInt( FormatDateTime('yyyy',DateTimePicker1.Date));
       Month :=StrToInt(FormatDateTime('mm',DateTimePicker1.Date));
       Day :=StrToInt( FormatDateTime('dd',DateTimePicker1.Date));

      with ADODataSet1 do
      begin
         FieldByName('ID').value:=I;
         if I>=86400 then
           begin
            n :=I/86400;
            decodedate(DateTimePicker1.Date,Year,Month,Day);
            Day:=Day+n;
            FieldByName('Times').value:=IntToStr(Year)+'-'
                +IntToStr(Month)+'-'
                +IntToStr(Day)+' '+SecondToTime(I);

           end
         else
         begin
           decodedate(DateTimePicker1.Date,Year,Month,Day);
            FieldByName('Times').value:=IntToStr(Year)+'-'
                +IntToStr(Month)+'-'
                +IntToStr(Day+n)+' '+SecondToTime(I);

         end;
         FieldByName('Data').value:=Num[I];
         FieldByName('JsjgCode').value:='06';
      end;

       ADODataSet1.Requery();
      i:=i+1;
   end;
    RzLabel3.Caption:='写数完成!';
   ShowMessage('数据保存成功');
    RzLabel3.Caption:='';

end;

end.

运行时界面

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值