1. try begin ADOQuery1.Close(); ADOQuery1.SQL.Text := 'select * from table1'; ADOQuery1.Open(); end; except begin ...没有该表 end; 2. 用ADOConnection ADOConnection1.GetTableNames(ComboBox1.Items,false) 把该ADOConnction连的数据库中你的表名都得到了,放到ComboBox1中,下面在判断你的表是不是在里面
Adoconnection1.gettablenames(listbox1.items,false); if listbox1.items.indexof(tablename)<>-1 then //存在 else //不存在
用如下的函数,在使用之前请确保AdoQuery1与AdoConnection相连并且AdoConnection已经打开 function TForm1.IsExist(tb:String):Boolean; var sql:String; begin sql:='select * from sysobjects where id=object_id('''+tb+''')'; AdoQuery1.SQL.Clear; AdoQuery1.SQL.Add(sql); adoQuery1.Open; if AdoQuery1.Recordset.EOF then IsExist:=False else IsExist:=True; end;
sql:='select * from sysobjects where id=object_id('''+tb+''')';楼上的,这样adoQuery1.Open;如果表不存在会报错吧?
sql:='select * from sysobjects where id=object_id('''+tb+''') and xtype = ''U''';
try
begin
ADOQuery1.Close();
ADOQuery1.SQL.Text := 'select * from table1';
ADOQuery1.Open();
end;
except
begin
...没有该表
end;
2.
用ADOConnection
ADOConnection1.GetTableNames(ComboBox1.Items,false)
把该ADOConnction连的数据库中你的表名都得到了,放到ComboBox1中,下面在判断你的表是不是在里面
Adoconnection1.gettablenames(listbox1.items,false);
if listbox1.items.indexof(tablename)<>-1 then
//存在
else
//不存在
function TForm1.IsExist(tb:String):Boolean;
var
sql:String;
begin
sql:='select * from sysobjects where id=object_id('''+tb+''')';
AdoQuery1.SQL.Clear;
AdoQuery1.SQL.Add(sql);
adoQuery1.Open;
if AdoQuery1.Recordset.EOF then
IsExist:=False
else
IsExist:=True;
end;
请问
AustinLei
firetoucher
没有报错啊,不管表是不是存在都不会报错,我试过了。
xtype = ''U''';
又是什么意思?
xtype='U' 表
xtype='V'视图
如果是其他数据库还是用Adoconnection的遍历进行判断吧