如何检查表(/库)是否存在而动态建表(/库)呢(30分)

  • 主题发起人 主题发起人 dali2000
  • 开始时间 开始时间
D

dali2000

Unregistered / Unconfirmed
GUEST, unregistred user!
我的表是要动态建立的,也就是说要先检查表是否存在,不存在则建立。如何编程呢?
 
try
query.close;
query.sql.clear;
query.sql.add(select statement);
query.open;
except
query.close;
query.sql.clear;
query.sql.add(create table statement);
query.open;
end;
 
动态建立数据库(SQL server上创建)
adoquery2.close;
adoquery2.SQL.Clear ;
adoquery2.SQL.Add('select * from sysdatabases where name=:name1');
adoquery2.Parameters[0].Value:=库名;
adoquery2.open;
if adoquery2.recordcount>0 then
begin
application.MessageBox('这个数据库名已经存在!','提示信息',mb_ok+mb_iconinformation);
exit;
end;
if adoquery2.Active=true then
try
adoquery2.Recordset.Close ;
adoquery2.Close ;
except
adoquery2.Close;
end;
adoquery2.SQL.Clear ;
adoquery2.SQL.Add('Create database 库名');
adoquery2.ExecSQL ;
adoquery2.Close ;
动态建表:
adoquery1.Close;
adoquery1.SQL.Clear ;
adoquery1.SQL.Add('CREATE TABLE 表名 ( [字段名] [数据类型] (长度) NOT NULL)');
adoquery1.ExecSQL ;
 
同意bingjian
 
procedure TDM.CreateMDB(DBName: string); // 系统需要安装 MADAC
var
CreateAccess: OleVariant;
begin
CreateAccess := CreateOleObject('ADOX.Catalog');
CreateAccess.Create('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + DBName);
end;

procedure TDM.DataModuleCreate(Sender: TObject);
procedure ExtractStr(const ConStr: string);
var
s: string;
mPos: Integer;
begin
s := Constr + ';';
with TStringlist.Create do
begin
try
while Pos(';', s) > 0 do
begin
mPos := Pos(';', s);
Add(Copy(s, 1, mPos - 1));
System.Delete(s, 1, mPos);
end;
FMacineName := Values['Data Source'];
FDBName := Values['Initial Catalog'];
finally
Free;
end;
end;
end;
function DBExists: Boolean;
begin
with ads_Pub do
begin
Close;
CommandText := 'SELECT * FROM sysdatabases WHERE name = ''' + FDBName + '''';
Open;
if isEmpty then
Result := False
else
Result := True;
end;
end;
function CreateSQLServerDB: Boolean;
begin
cmd_Pub.CommandText := 'Create database ' + FDBName;
{fanghe on ' +
'( Name = fanghe_data, FileName = ''c:/test.mdf'',' +
'size = 15, MaxSize = 20, FileGrowth = 5 ) ' +
'Log on ( Name = fanghe_log, FileName = ''c:/test.1df'',' +
'Size = 3, MaxSize = 10, FileGrowth = 1 )';}
try
cmd_Pub.Execute;
Result := True;
except
Result := False;
end;
end;
var
TableSt: TStrings;
sfile: string;
begin
SCreen.Cursor := crHourGlass;
try
TableSt := TStringList.Create;
Sfile := CurDir + 'Datapath';
isServer := False;
if FileExists(sfile) then
begin
TableSt.LoadFromFile(Sfile);
ExtractStr(TableSt.Text);
ADOConn.ConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;' +
'User ID=sa;Initial Catalog=master;Data Source=' + FMacineName;
ADOConn.Connected := True;
if not DBExists then
if not CreateSQLServerDB then
begin
Application.MessageBox('数据库创建失败!', '系统信息', 0);
Exit;
end;
ADOConn.Connected := False;
ADOConn.ConnectionString := TableSt.Text;
isServer := True;
end
else
begin
if not DirectoryExists(CurDir + 'Data') then
ForceDirectories(CurDir + 'Data');
Sfile := CurDir + 'Data/Account.mdb';
if not FileExists(Sfile) then
begin
try
CreateMDB(Sfile);
except
Application.MessageBox('数据库创建失败!', '系统信息', 0);
Exit;
end;
ADOConn.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;' +
'Data Source=./Data/Account.mdb;Mode=Share Deny None;Extended Properties="";' +
'Jet OLEDB:System database="";Jet OLEDB:Registry Path="";' +
'Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=5;' +
'Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;' +
'Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";' +
'Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;' +
'Jet OLEDB:Don''t Copy Locale on Compact=False;' +
'Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False';
end;
end;
try
ADOConn.Connected := True;
except
Application.MessageBox('数据库连接失败!', '系统信息', 0);
Exit;
end;
TableSt.Clear;
try
ADOConn.GetTableNames(TableSt);
if TableSt.IndexOf('GlobeParam') = -1 then
SQLBat.RunScript;
finally
TableSt.Free;
end;
finally
Screen.Cursor := crDefault;
end;
end;
 
try ...except
 
后退
顶部