Delphi ADOQuery

Delphi ADOQuery

procedure TForm1.Button1Click(Sender: TObject); 
var 
A: Array of String;//定义动态数组 
Index: Integer;//定义数组下标变量
ADOQuery:TADOQuery; 
begin 
ADOQuery:=TADOQuery.create;
ADOQuery.SQL.Clear; 
ADOQuery.SQL.Add('Select B from A'); 
ADOQuery.Open; 
Setlength(A,ADOQuery.RecordCount); //设置数组宽度 
Index := 0;//初始化下标 
ADOQuery.First; 
While Not ADOQuery.Eof Do //数据集循环 
begin 
A[Index] := ADOQuery.FieldByName('B').asString;//数据添加到数组中 Inc(Index); 
ADOQuery.Next; 
end; 
end;
View Code

Delphi ADOQuery连接数据库的查询、插入、删除、修改

//查询记录
procedure TForm1.Button1Click(Sender: TObject);
begin
ADOQuery.Close;
ADOQuery.SQL.Clear;
ADOQuery.SQL.Add('select * from YourTABLE where 查询条件');
ADOQuery.Open;

//插入记录
procedure TForm1.Button2Click(Sender: TObject);
begin
ADOQuery.Close;
ADOQuery.SQL.Clear;
ADOQuery.SQL.Text:='insert into YourTABLE(字段1,字段2) values(:字段1,:字段2)';
// ADOQuery.SQL.Add('insert into YourTABLE values(:字段1)');
ADOQuery.Parameters.ParamByName('字段1').Value:=trim(Edit1.Text);
ADOQuery.Parameters.ParamByName('字段2').Value:=trim(Edit2.Text);
ADOQuery.ExecSQL;
end;
//删除记录
procedure TForm1.Button3Click(Sender: TObject);
begin
ADOQuery.Close;
ADOQuery.SQL.Clear;
ADOQuery.SQL.Text:='Delete from YourTABLE where 字段3=:字段3';
//这里没有添加where的条件判断,实际使用时,注意添加判断
// ADOQuery.SQL.Add('Delete from NEW_TABLE where 字段3=:字段3');
ADOQuery.Parameters.ParamByName('字段3').Value:=trim(Edit3.Text);
ADOQuery.ExecSQL;
//删除记录也可用DeleteRecords()函数
procedure DeleteRecords(AffectRecords: TAffectRecords = arAll);   
这个函数有一个参数:AffectRecords可以取如下的值:   
1、arCurrent :删除当前记录   
2、arFiltered :删除符合Filter过滤后的所有记录(如果你使用Filter过滤的话)   
3、arAll          :删除所有记录   
4、arAllChapters :Delete affects all chapters(ADO chapters)
//修改记录
procedure TForm1.Button4Click(Sender: TObject);
begin
ADOQuery.Close;
ADOQuery.SQL.Clear;
ADOQuery.SQL.Text:='Update YourTABLE SET 字段4=:字段4';
//这里没有添加where的条件判断,实际使用时,注意添加判断
// ADOQuery.SQL.Add('Update YourTABLE SET 字段4=:字段4');
ADOQuery.Parameters.ParamByName('字段4').Value:=trim(Edit4.Text);
ADOQuery.ExecSQL;
//即时更新插入、删除、修改后的记录
在上面插入、删除、修改的语句后添加如下代码即可:
ADOQuery.Close;
ADOQuery.SQL.Add('select * from YourTABLE where 查询条件');
ADOQuery.Open;
//使用ADOQuery时注意:

1、如果你需要更改数据,query.requestlive必须为true
2、如果有输入参数的时候,容易出错,通常的错法是这样:
比如:“WHERE abc = : abc”
改正如下:“WHERE abc=:abc”就是说=:前后都不能留空格。
3、ADOQuery.Open与ADOQuery.ExecSQL 有不同之处。
ADOQuery.Open一般用在查询,select时候;而ADOQuery.ExecSQL用在insert,delete,update等。
View Code

Delphi中使用ADO连接带密码的Access

Delphi中使用ADO连接带密码的Access

 

var gDBConn:string;


try
self.ADOConnection1.Close;
//无密码连接方式 Password="";
//gDBConn:='Provider=Microsoft.Jet.OLEDB.4.0;Password="";Data Source=MyDB.mdb;Persist Security Info=True';
//有密码连接方式 Jet OLEDB:Database Password=delphi7;
gDBConn:='Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=123456;Data Source='+ExtractFilePath(application.ExeName)+'MyDB.mdb;Persist Security Info=True';
self.ADOConnection1.ConnectionString := gDBConn;
self.ADOConnection1.Open;
except
//raise;
self.ADOConnection1.Close;
self.ADOConnection1.Free;
application.MessageBox('不能连接到数据库!','提示信息',64);
application.Terminate;
end; 
View Code

delphi获取库中所有表名,表字段名

var   
      ts:Tstrings;   
      i:integer;   
  begin   
      ts:=Tstringlist.Create;   
      //adoconnection1.GetTableNames(ts,false);   
      //for   i:=0   to   ts.Count-1   do   
      //   showmessage('第'+inttostr(i+1)+'个表名是'+ts.Strings);   
      adoconnection1.GetFieldNames('stu',ts);   
      for   i:=0   to   ts.Count-1   do   
              showmessage(''+inttostr(i+1)+'个字段名是'+ts.Strings);   
  end;   
    //注:用session控件也可以完成以上的功能
View Code

猜你喜欢

转载自www.cnblogs.com/blogpro/p/11345806.html