DELPHI操作ACCESS数据库
1.DELPHI连接ACCESS数据库2.DELPHI向ACCESS数据库添加数据3.DELPHI删除数据4.修改数据一楼的只回答了第一个我要全的。要写要设置的属性。OK...
1.DELPHI连接ACCESS数据库
2.DELPHI向ACCESS数据库添加数据
3.DELPHI删除数据
4.修改数据
一楼的只回答了第一个
我要全的。
要写要设置的属性。
OK? 展开
2.DELPHI向ACCESS数据库添加数据
3.DELPHI删除数据
4.修改数据
一楼的只回答了第一个
我要全的。
要写要设置的属性。
OK? 展开
2个回答
展开全部
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ADODB, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
FConnection: TADOConnection;
FDataSet: TADOQuery;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
ActiveX;
const
CONN_JETOLE_P = 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=%s;Persist Security Info=False;' +
'Jet OLEDB:Database Password=%s';
//添加数据
procedure TForm1.Button1Click(Sender: TObject);
begin
FDataSet.SQL.Text := 'SELECT * FROM [表名] WHERE 1=0';
FDataSet.Open; //打开
FDataSet.Append; //开始添加数据
FDataSet.FieldByName('字段名1').Value := '值';
FDataSet.Post; //提交,使添加生效
end;
//修改数据
procedure TForm1.Button3Click(Sender: TObject);
begin
FDataSet.SQL.Text := 'SELECT * FROM [表名] WHERE 条件';
FDataSet.Open; //打开
FDataSet.Edit; //开始修改数据
FDataSet.FieldByName('字段名1').Value := '值';
FDataSet.Post; //提交,使添加生效
end;
//删除数据
procedure TForm1.Button2Click(Sender: TObject);
begin
FDataSet.SQL.Text := 'SELECT * FROM [表名] WHERE 条件';
FDataSet.Open; //打开
FDataSet.Delete; //删除当前数据
//也可用 SQL 语句方法删除
FDataSet.SQL.Text := 'DELETE FROM [表名] WHERE 条件';
FDataSet.ExecSQL; //执行 SQL 语句
end;
constructor TForm1.Create(AOwner: TComponent);
begin
inherited;
FConnection := TADOConnection.Create(nil);
FConnection.LoginPrompt := False; //不弹出口令对话框
FConnection.ConnectionString := Format(CONN_JETOLE_P, ['MDB 文件名', '密码,如果没有,则留空']);
FDataSet := TADOQuery.Create(nil);
FDataSet.Connection := FConnection;
end;
destructor TForm1.Destroy;
begin
FreeAndNil(FConnection);
FreeAndNil(FDataSet);
inherited;
end;
initialization
ActiveX.CoInitialize(nil);
finalization
ActiveX.CoUninitialize;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ADODB, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
FConnection: TADOConnection;
FDataSet: TADOQuery;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
ActiveX;
const
CONN_JETOLE_P = 'Provider=Microsoft.Jet.OLEDB.4.0;' +
'Data Source=%s;Persist Security Info=False;' +
'Jet OLEDB:Database Password=%s';
//添加数据
procedure TForm1.Button1Click(Sender: TObject);
begin
FDataSet.SQL.Text := 'SELECT * FROM [表名] WHERE 1=0';
FDataSet.Open; //打开
FDataSet.Append; //开始添加数据
FDataSet.FieldByName('字段名1').Value := '值';
FDataSet.Post; //提交,使添加生效
end;
//修改数据
procedure TForm1.Button3Click(Sender: TObject);
begin
FDataSet.SQL.Text := 'SELECT * FROM [表名] WHERE 条件';
FDataSet.Open; //打开
FDataSet.Edit; //开始修改数据
FDataSet.FieldByName('字段名1').Value := '值';
FDataSet.Post; //提交,使添加生效
end;
//删除数据
procedure TForm1.Button2Click(Sender: TObject);
begin
FDataSet.SQL.Text := 'SELECT * FROM [表名] WHERE 条件';
FDataSet.Open; //打开
FDataSet.Delete; //删除当前数据
//也可用 SQL 语句方法删除
FDataSet.SQL.Text := 'DELETE FROM [表名] WHERE 条件';
FDataSet.ExecSQL; //执行 SQL 语句
end;
constructor TForm1.Create(AOwner: TComponent);
begin
inherited;
FConnection := TADOConnection.Create(nil);
FConnection.LoginPrompt := False; //不弹出口令对话框
FConnection.ConnectionString := Format(CONN_JETOLE_P, ['MDB 文件名', '密码,如果没有,则留空']);
FDataSet := TADOQuery.Create(nil);
FDataSet.Connection := FConnection;
end;
destructor TForm1.Destroy;
begin
FreeAndNil(FConnection);
FreeAndNil(FDataSet);
inherited;
end;
initialization
ActiveX.CoInitialize(nil);
finalization
ActiveX.CoUninitialize;
end.
展开全部
1、采用ADO方式连接,直接文件连接,放入控件即可;
2、使用ADO后,可用相应的类完成SQL语句。
Query: TADOQuery;
Query:= TADOQuery.create(nil);
Query.Connection := adoConnect;
Query.SQL.Clear;
Query.SQL.Add('select * from tab_judge');
try
Query.Open;
except
showmessage('打开数据库错!');
Query.Free;
Exit;
end; //end of try
Query.First;
....
2、使用ADO后,可用相应的类完成SQL语句。
Query: TADOQuery;
Query:= TADOQuery.create(nil);
Query.Connection := adoConnect;
Query.SQL.Clear;
Query.SQL.Add('select * from tab_judge');
try
Query.Open;
except
showmessage('打开数据库错!');
Query.Free;
Exit;
end; //end of try
Query.First;
....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询