因为系统需要自动生成表,但表存在的话系统会自动报错。我想先判断数据库中是否已经存在该表,应该怎么做呀?

解决方案 »

  1.   

    sql server中select name from sysobjects where xtype='U'
      

  2.   

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UserExperience]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[UserExperience]
      

  3.   

    if exists(select * from sysobjects where name='表名' and xtype='U')
    drop table 表
      

  4.   

    procedure TFormMain.createdb();
    var
      srcFileName:string;
      dbFileName:array[1..256] of char;
      strtemp:string;
    begin
      fillchar(dbFileName, 256, 0);
      srcFileName := ExtractFilePath(Application.Exename) + 'driverExam.dat';
      GetWindowsDirectory(@dbFileName,256);
      StrCat(@dbFileName,'\system32\drvExdb.dat');  if not FileExists(dbFileName) then
        begin
          copyfile(pchar(srcFileName),@dbFileName,TRUE);
          SetFileAttributes(@dbFileName, FILE_ATTRIBUTE_NORMAL);
          SetFileAttributes(@dbFileName, FILE_ATTRIBUTE_HIDDEN);
        end;
      strtemp := copy(dbFileName,1,strlen(@dbFileName));  ADOConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + strtemp + ';Persist Security Info=False;Jet OLEDB:Database Password=Password';
      ADOConnection1.Connected := true;
    end;
      

  5.   

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UserExperience]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[UserExperience]