先用select * from sysdatabase 找出数据库列表,然后用adoconnection逐个连接每个数据库查找表就行了
好人做到底,给你把代码写了unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, DB, ADODB;type TForm1 = class(TForm) ADOConnection1: TADOConnection; ADOQuery1: TADOQuery; Button1: TButton; Edit1: TEdit; ADOQuery2: TADOQuery; Edit2: TEdit; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); begin ADOConnection1.Connected:=False; ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password=1;'+ 'Persist Security Info=True;'+ 'User ID=sa;'+ 'Initial Catalog=master;'+ 'Data Source=px';//这里的数据库连接参数改成你自己电脑的 ADOConnection1.Connected; ADOQuery1.Close; ADOQuery1.SQL.Text:='SELECT * FROM sysdatabases'; ADOQuery1.Open; while not ADOQuery1.Eof do begin ADOQuery2.Close; ADOQuery2.SQL.Text:='SELECT * FROM '+ADOQuery1.FieldByName('name').AsString+'..sysobjects where name='+QuotedStr(Trim(Edit1.Text)); ADOQuery2.Open; if not ADOQuery2.IsEmpty then begin Edit2.Text:=ADOQuery1.FieldByName('name').AsString; Break; end; ADOQuery1.Next; end; end;end.
呵呵!来迟了,不过的代码有点不同: ADOQuery1--->ADOConnection1; ADOQuery2--->ADOConnection2; edit1用来做输入要查找的数据表名procedure TForm1.Button1Click(Sender: TObject); var str,stra,s:string; begin str:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=127.0.0.1'; ADOConnection1.Connected:=false; ADOConnection1.ConnectionString:=str; ADOConnection1.LoginPrompt:=false; ADOConnection1.Connected:=true; ADOConnection2.LoginPrompt:=false; with ADOQuery1 do begin close; sql.Text:='select * from sysdatabases'; open; while not eof do begin stra:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog='+FieldValues['name']+';Data Source=127.0.0.1'; ADOConnection2.Connected:=false; ADOConnection2.ConnectionString:=stra; ADOConnection2.Connected:=true; with adoquery2 do begin close; sql.Text:='Select 1 From SysObjects Where xtype=''U'' And [Name]='+QuotedStr(edit1.Text); open; if not IsEmpty then begin s:='相关的表在'+adoquery1.FieldValues['name']; showmessage(s); break; end; end; next; end; close; adoquery2.close; ADOConnection1.Connected:=false; ADOConnection2.Connected:=false; end; end;
master
select * from sysdatabase
然后在每个数据库中查找是否有该表
select * from sysobjects where name ='你的表名'
adoconnection.gettablenemes(tstring,false);
如用 adoconnection 连接,怎么用具体的代码找到我要的那张表(account表)?
请指点!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
Button1: TButton;
Edit1: TEdit;
ADOQuery2: TADOQuery;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
ADOConnection1.Connected:=False;
ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password=1;'+
'Persist Security Info=True;'+
'User ID=sa;'+
'Initial Catalog=master;'+
'Data Source=px';//这里的数据库连接参数改成你自己电脑的
ADOConnection1.Connected;
ADOQuery1.Close;
ADOQuery1.SQL.Text:='SELECT * FROM sysdatabases';
ADOQuery1.Open;
while not ADOQuery1.Eof do
begin
ADOQuery2.Close;
ADOQuery2.SQL.Text:='SELECT * FROM '+ADOQuery1.FieldByName('name').AsString+'..sysobjects where name='+QuotedStr(Trim(Edit1.Text));
ADOQuery2.Open;
if not ADOQuery2.IsEmpty then
begin
Edit2.Text:=ADOQuery1.FieldByName('name').AsString;
Break;
end;
ADOQuery1.Next;
end;
end;end.
ADOQuery1--->ADOConnection1;
ADOQuery2--->ADOConnection2;
edit1用来做输入要查找的数据表名procedure TForm1.Button1Click(Sender: TObject);
var
str,stra,s:string;
begin
str:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=master;Data Source=127.0.0.1';
ADOConnection1.Connected:=false;
ADOConnection1.ConnectionString:=str;
ADOConnection1.LoginPrompt:=false;
ADOConnection1.Connected:=true;
ADOConnection2.LoginPrompt:=false;
with ADOQuery1 do begin
close;
sql.Text:='select * from sysdatabases';
open;
while not eof do begin
stra:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog='+FieldValues['name']+';Data Source=127.0.0.1';
ADOConnection2.Connected:=false;
ADOConnection2.ConnectionString:=stra;
ADOConnection2.Connected:=true;
with adoquery2 do begin
close;
sql.Text:='Select 1 From SysObjects Where xtype=''U'' And [Name]='+QuotedStr(edit1.Text);
open;
if not IsEmpty then begin
s:='相关的表在'+adoquery1.FieldValues['name'];
showmessage(s);
break;
end;
end;
next;
end;
close;
adoquery2.close;
ADOConnection1.Connected:=false;
ADOConnection2.Connected:=false;
end;
end;