⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 Delphi7编程80例(完全版)
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBTables, StdCtrls, Grids, DBGrids;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var Str:String;
 TxtFile:TextFile;//定义一个文本文件变量TxtFile
 Ch:Char;
begin
   {设置Table1的属性}
 Table1.DatabaseName:=ExtractFilePath(ParamStr(0));
 Table1.TableName:='friends.db';
 Table1.open;
  //将friends.txt分配给TxtFile变量
 AssignFile(TxtFile,ExtractFilePath(ParamStr(0))+'friends.txt');
 Reset(TxtFile);               //打开friends.txt
 Str:='';
 while not Eof(TxtFile) do    //循环读取文本内容
 begin
  Read(TxtFile,Ch);           //从friends.txt中读一个字符
  if Ch<>Char(13)then         //是否为回车键
   Str:=Str+Ch                //不是回车键,继续读下一个字符
  else
  begin                      //是回车符,表明已经读完一行,此时Str变量即代表一行信息
   Table1.Append;            //追加记录
   Table1.Edit;              //编辑friends.db
     {读取文本内容到四个字段中。关键:顶格的行列号是(1,1)}
   Table1.FieldByName('姓名').AsString:=Copy(Str,1,8);
   Table1.FieldByName('性别').AsString:=Copy(Str,9,2);
   Table1.FieldByName('年龄').AsString:=Copy(Str,13,2);
   Table1.FieldByName('省份').AsString:=Copy(Str,15,8);
   Table1.Post;       //将数据过至friends.db中
   Read(TxtFile,Ch); //略过换行符
   Str:='';          //清Str变量
   end ;
 end;
 Button2.Enabled:=True;
end;

procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
    str:string;
begin
 Memo1.clear;
 for i:=0 to Table1.FieldCount-1 Do
   str:=str+Table1.Fields[i].FieldName+'  '; //读取字段
 Memo1.Lines.Add(str);
 Memo1.Lines.Add('');
 str:='';
  
 {循环读取表格中的内容}
 Table1.First;
 While not Table1.Eof Do
  Begin
  for i:=0  To Table1.FieldCount-1 Do
    str:=str+Table1.Fields[i].AsString+'  ';  //读取数据记录
  Memo1.Lines.Add(str);
  Memo1.Lines.Add('');
  str:='';
  Table1.Next;
  end;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -