Delphi 范例自学手册[2] 学生表的创建 实例002,实例003

接实例1的内容。这次在上次建好的数据库中创建一个学生表。

先只用一个TQuery组件。将他的databasename设好。

这个是BDE Administrator中的选项。所以BDE Administrator也要设一下。

上一次

中的DATABASE NAME没有设 因为是创建数据库。

这一次不一样了要把名字弄好。然后保存打开一下。都变成黑体就ok了。

之后是主代码

先上窗体

unit Unit1;

interface

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

type
TForm1
= class(TForm)
qry1: TQuery;
btn1: TBitBtn;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
begin
try
with qry1 do
begin
Close;
SQL.Clear;
SQL.Add(
'Create Table 学生表');
SQL.Add(
'(学生编号 Varchar(20) not null,学生姓名 Varchar(20),语文成绩 int,');
SQL.Add(
'数学成绩 int, 班主任 Varchar(20))');
ExecSQL;
end;
Application.MessageBox(
'数据表创建成功。', '提示', MB_OK);
except
Application.MessageBox(
'数据表创建失败。', '提示', MB_OK);
end;

end;

end.

这样就可以了。

下面尝试另一种方法。

加了个Tdatabase

直接用图说话:

这是TDataBase组件的属性

这是TQuery的

代码如下:

unit eg002;

interface

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

type
TForm1
= class(TForm)
qry1: TQuery;
btn1: TBitBtn;
db1: TDatabase;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
begin
try
with qry1 do
begin
Close;
SQL.Clear;
SQL.Add(
'Create Table 学生表');
SQL.Add(
'(学生编号 Varchar(20) not null,学生姓名 Varchar(20),语文成绩 int,');
SQL.Add(
'数学成绩 int, 班主任 Varchar(20))');
ExecSQL;
end;
Application.MessageBox(
'数据表创建成功。', '提示', MB_OK);
except
Application.MessageBox(
'数据表创建失败。', '提示', MB_OK);
end;

end;

end.

以上是实例002的内容。实例003 就是SQL语句变一下  教师编号 Varchar(20) not null,Constraint aaaa primaty key('教师编号')

提出的问题:

TDatabase到底有啥用?只是方便连接吗?不用连接提示框吗?

原文地址:https://www.cnblogs.com/ljjphysics/p/2053824.html