用sql进行表的插入数据行,空白行,删除行,修改行

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ADOConnection1: TADOConnection;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button2: TButton;
    ADOQuery1: TADOQuery;
    Button1: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
//插入一行数据
procedure TForm1.Button1Click(Sender: TObject);
begin
  ADOQuery1.SQL.Text := 'insert into [学生表] ([姓名],[性别],[生日],[年龄],[系号]) values'
                        +'(''张三'',''男'',''1900-01-01'',''18'',''D01'')';                                //+'(''张三'',''男'',''1981-08-16'',''18'',''D01'')';
  ADOQuery1.ExecSQL;
  ADOQuery1.SQL.Text := 'select * from [学生表]';
  ADOQuery1.Active := True;
  DataSource1.DataSet := ADOQuery1;
  DBGrid1.DataSource := DataSource1;
end;
//建立数据表
procedure TForm1.Button2Click(Sender: TObject);
begin
  try
//    ADOConnection1.Connected := False;
//    ADOConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;'
//                                      +'Data Source=001.mdb;'
//                                      +'Persist Security Info=False';
//    ADOConnection1.LoginPrompt := False;
//    ADOConnection1.Connected := True;
//    ADOQuery1.Connection := ADOConnection1;
    ADOQuery1.SQL.Text := 'create table [学生表]'
                         +'([编号]  identity (1,1) primary key,'
                         +'[姓名] char (5),'
                         +'[性别] char (1),'
                         +'[生日] datetime,'
                         +'[年龄] smallint,'
                         +'[系号] char (10))';
    ADOQuery1.ExecSQL;
    ADOQuery1.SQL.Text := 'select * from [学生表]';
    ADOQuery1.Active := True;
    DataSource1.DataSet := ADOQuery1;
    DBGrid1.DataSource := DataSource1;   
  except
     ShowMessage('已经存在学生表了!');
  end;
end;
//删除数据表
procedure TForm1.Button3Click(Sender: TObject);
begin
  try
    ADOQuery1.SQL.Text := 'drop table [学生表]';
    ADOQuery1.ExecSQL;
    ADOQuery1.Active := True;
    DataSource1.DataSet := ADOQuery1;
    DBGrid1.DataSource := DataSource1;
    ShowMessage('学生表已经删除!');    
  except
    ShowMessage('已经没有学生表了!') 
  end;

end;
//删除一行数据
procedure TForm1.Button4Click(Sender: TObject);
  var i : string;
begin
    if not ADOQuery1.IsEmpty then
    begin
    i := ADOQuery1.FieldValues['编号'];
    ADOQuery1.SQL.Text := 'delete from [学生表] where[编号]='+i+'';
    ADOQuery1.ExecSQL;
    ADOQuery1.SQL.Text := 'select * from [学生表]';
    ADOQuery1.Active := True;
    DataSource1.DataSet := ADOQuery1;
    DBGrid1.DataSource := DataSource1;
    end
     else 
    ShowMessage('[学生表]中没有数据了!');
end;
//修改一行数据
procedure TForm1.Button5Click(Sender: TObject);
 var bian,xingbie: string;
begin
  if ADOQuery1.FieldValues['系号'] = null then
    begin
      Application.MessageBox('当前行系号是空的!无法修改!','提示',0);
      exit;
    end;
    bian := ADOQuery1.FieldValues['编号'];
    xingbie := ADOQuery1.FieldValues['性别'];
    ADOQuery1.SQL.Text := 'update [学生表] set [系号]=''修改为D02'' where [性别]= '''+xingbie+''' and [编号]='+bian+'';
    ADOQuery1.ExecSQL;
    ADOQuery1.SQL.Text := 'select * from [学生表]';
    ADOQuery1.Active := True;
    DataSource1.DataSet := ADOQuery1;
    DBGrid1.DataSource := DataSource1;
end;
//插入空白行
procedure TForm1.Button6Click(Sender: TObject);
begin
    ADOQuery1.SQL.Text := 'insert into [学生表] ([姓名],[性别],[生日],[年龄],[系号]) values'
                        +'(Null,Null,Null,Null,Null)';                                //+'(''张三'',''男'',''1981-08-16'',''18'',''D01'')';
  ADOQuery1.ExecSQL;
  ADOQuery1.SQL.Text := 'select * from [学生表]';
  ADOQuery1.Active := True;
  DataSource1.DataSet := ADOQuery1;
  DBGrid1.DataSource := DataSource1;
end;

//程序创建时
procedure TForm1.FormCreate(Sender: TObject);
begin
  try
    try
     ADOConnection1.ConnectionString := 'provider = microsoft.jet.oledb.4.0;'
                                       +'data source = 001.mdb;'
                                       +'persist security info = false';
     ADOConnection1.LoginPrompt := False;
     ADOConnection1.Connected := True;
    except 
      ShowMessage('没有发现数据库!')
    end;
    ADOQuery1.Connection := ADOConnection1;
    ADOQuery1.SQL.Text := 'select * from [学生表]';
    ADOQuery1.Active := True;
    DataSource1.DataSet := ADOQuery1;
    DBGrid1.DataSource := DataSource1;
  except
    ShowMessage('没有发现数据表!');
  end;

end;

end.

你可能感兴趣的:(delphi程序设计)