建立 Access 數據庫 mdb 數據庫(delphi)

 


function CreateAccessDB(MDBFile: string): Boolean;數據庫

  function CreateAccessFile(FileName: String; PassWord: string=''): boolean;
  var
    vCatalog: OleVariant;
  begin
    Result := False;
    if FileExists(FileName) then begin
      gLastError := '表"'+ FileName + '"已經存在.';
      Exit;
    end;
    try
      vCatalog := CreateOleObject('ADOX.Catalog');
      vCatalog.Create(format(MDBConnectionString, [FileName, PassWord]));
      Result := True;
    except
      on E: Exception do gLastError := E.Message;
    end;
  end;code

  function CreateFieldTable(TableName: string): boolean;
  var
    ADOCon: TADOConnection;
    AdoQry: TADOQuery;
    TblLst: TStringList;
    idx: Integer;
  begin
    Result := False;
    ADOCon := TADOConnection.Create(nil);
    ADOCon.ConnectionString := format(MDBConnectionString, [MDBFile, '']);
    ADOCon.LoginPrompt := False;
    AdoQry := TADOQuery.Create(nil);
    TblLst := TStringList.Create;
    try
      try
        ADOCon.Connected := True;
        ADOCon.GetTableNames(TblLst);orm

        if TblLst.Find('Tab1', idx) then begin
          gLastError := '表' + 'Tab1' + '已經存在!';
          Exit;
        end else begin
          AdoQry.Connection := ADOCon;
          AdoQry.SQL.Text :=
            'Create Table [' + TableName + '] (' +
//            '[ID]   counter,' +
            '[FieldName] string,'  +
            '[ViewX] integer,' +
            '[ViewY] integer,' +
            '[X1] integer,' +
            '[Y1] integer,' +
            '[X2] integer,' +
            '[Y2] integer)';
          try
            AdoQry.ExecSQL;
            Result := True;
          except
            on e: Exception do
              gLastError := '建立表' + '"Tab1"' + '失敗!(' + e.Message + ')';
          end;
          AdoQry.SQL.Text :=
            'Create Unique Index iFieldName ON [' + TableName + ']([FieldName])';
          try
            AdoQry.ExecSQL;
            Result := True;
          except
            on e: Exception do
              gLastError := '建立表' + '"Tab1"' + '失敗!(' + e.Message + ')';
          end;
        end;
      except
        on e: Exception do
          gLastError := '打開數據庫' + MDBFile + '失敗!(' + e.Message + ')';
      end;
    finally
      TblLst.Free;
      AdoQry.Free;
      ADOCon.Free
    end;
  end;string

begin
  if not CreateAccessFile(MDBFile) then begin
    writelog(gLastError);
    Exit;
  end;it

  CreateFieldTable('FieldInfo');
end;io

function OpenTable(MDBFile, TableBame: string): TQuery;
var
  qry: TADOQuery;
begin
  qry := TADOQuery.Create(nil);
  qry.ConnectionString := format(MDBConnectionString, [MDBFile, '']);
  qry.SQL.Text := 'select * from [' + TableBame + ']';
  qry.Open;
  Result := TQuery(qry);
end;ast

相關文章
相關標籤/搜索