RSS订阅 | 匿名投稿
您的位置:网站首页 > 数据库 > 正文

delphi学习SQL语句 - Insert、Update、Delete

作者:admin 来源:未知 日期:2010/1/27 9:07:45 人气: 标签:access

unit Unit1;

interface

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

type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
ADODataSet1: TADODataSet;
ADOCommand1: TADOCommand;
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

//添加
procedure TForm1.Button1Click(Sender: TObject);
begin
try
ADOCommand1.CommandText := 'INSERT INTO country VALUES("AAA", "BBB", "CCC", 111, 222)';
ADOCommand1.Execute;

ADOCommand1.CommandText :=
'INSERT INTO country(Name,Capital,Continent) VALUES("XXX", "YYY", "ZZZ")';
ADOCommand1.Execute;
except on E: Exception do
ShowMessage(E.Message);
end;
end;

//更新
procedure TForm1.Button2Click(Sender: TObject);
begin
ADOCommand1.CommandText := 'UPDATE country SET Area=123,Population=456 WHERE Name="AAA"';
ADOCommand1.Execute;

ADOCommand1.CommandText := 'UPDATE country SET Capital="MMM" WHERE Capital="YYY"';
ADOCommand1.Execute;
end;

//删除
procedure TForm1.Button3Click(Sender: TObject);
begin
ADOCommand1.CommandText := 'DELETE * From country WHERE Name="AAA"';
ADOCommand1.Execute;
ADOCommand1.CommandText := 'DELETE * From country WHERE Name="XXX"';
ADOCommand1.Execute;
end;

//查看
procedure TForm1.Button4Click(Sender: TObject);
begin
with ADODataSet1 do begin
Close;
CommandText := 'SELECT * FROM country';
Open;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
mdbFile: string;
begin
{因要修改数据库, 特复制了一份}
mdbFile := mdbFile + 'C:\Temp\dbdemos.mdb';

ADODataSet1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' +
mdbFile + ';Persist Security Info=False';

ADOCommand1.ConnectionString := ADODataSet1.ConnectionString;

DBGrid1.DataSource := DataSource1;
DataSource1.DataSet := ADODataSet1;
end;

end.
读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址: