unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Vcl.Grids, Vcl.DBGrids,
Data.Win.ADODB, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
Edit3: TEdit;
Label3: TLabel;
Edit4: TEdit;
Label4: TLabel;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select * from dbo.shangpin');
adoquery1.open;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
adoquery1.close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('insert into shangpin(id,name,price,time1)values ('''+Edit1.Text+''','''+Edit2.Text+''','''+Edit3.Text+''','''+Edit4.Text+''')');
adoquery1.ExecSQL;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select * from dbo.shangpin');
adoquery1.open;
showmessage('新的一个已经增加了');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('update shangpin set id='''+edit1.Text+''',name='''+edit2.Text+''',price='''+edit3.Text+''',time1='''+edit4.Text+''' where ID='''+edit1.Text+'''');
adoquery1.ExecSQL;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select * from dbo.shangpin');
adoquery1.open;
showmessage('新的一个已经修改了');
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('delete from shangpin where ID='''+Edit1.Text+'''');
adoquery1.ExecSQL;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select * from dbo.shangpin');
adoquery1.open;
showmessage('已经删除了第'''+Edit1.Text+'''');
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
Edit1.Text:='';
Edit2.Text:='';
Edit3.Text:='';
Edit4.Text:='';
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
adoquery1.SQl.Clear;
end;
end.
数据库控件:
1
adoconnection
2
adoquery
connection 选择上面配置好的那个内容
sql 随便写一点sql
active 连接好以后选择 true
3
datasoure
dataset 选择adoquery
4
dbgrid
datasoure 选择 配置好的datasoure
请发表评论