⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbimportdata.pas

📁 DBDesigner 4 is a database design system that integrates database design, modelling, creation and ma
💻 PAS
📖 第 1 页 / 共 2 页
字号:
      DestTblLU.ItemIndex:=0;
  finally
    theTables.Free;
  end;

  DefaultMapping.Text:=DefaultMappingData;

  DestTblLUCloseUp(self);
end;

procedure TDBImportDataForm.RefreshFileList;
var sr: TSearchRec;
begin
  if(ModePageControl.ActivePage=TextImportSheet)then
  begin
    //Show all files in current dir
    if(SourceDir<>'')then
    begin
      SourceLBox.Items.Clear;

      if(Copy(SourceDir, Length(SourceDir), 1)<>PathDelim)then
        SourceDir:=SourceDir+PathDelim;

      DirEd.Text:=SourceDir;

      //Add all Files
      if FindFirst(SourceDir+'*.*', faAnyFile, sr) = 0 then
      begin
        repeat
          if((sr.Attr and faDirectory)<>faDirectory)then
          begin
            SourceLBox.Items.Add(sr.Name);
          end;
        until FindNext(sr) <> 0;
        FindClose(sr);
      end;
    end;
  end
  else
  begin
    //Show DB-Tables
  end;
end;

procedure TDBImportDataForm.SetDirectoryOrFilename(dirOrFileName: string);
var i: integer;
begin
  SourceDir:=ExtractFilePath(dirOrFileName);

  RefreshFileList;

  if(FileExists(dirOrFileName))then
  begin
    for i:=0 to SourceLBox.Items.Count-1 do
      if(CompareText(SourceLBox.Items[i], ExtractFileName(dirOrFileName))=0)then
      begin
        SourceLBox.Checked[i]:=True;
        SourceLBox.ItemIndex:=i;

        //Set file
        SourceLBoxClick(self);

        break;
      end;
  end;

  DMMain.SaveValueInSettingsIniFile('RecentDirectories', 'RecentImportDataFileDir', ExtractFilePath(dirOrFileName));
end;

procedure TDBImportDataForm.BrowseDirOrDBConnBtnClick(Sender: TObject);
var DirDlg: TOpenDialog;
begin
  DirDlg:=TOpenDialog.Create(self);

  DirDlg.InitialDir:=DMMain.LoadValueFromSettingsIniFile('RecentDirectories', 'RecentImportDataFileDir', '');
  try
    //Get Dir
    DirDlg.FileName:='Select a File or Directory';
    if(DirDlg.Execute)then
    begin
      SetDirectoryOrFilename(DirDlg.FileName);
    end;
  finally
    DirDlg.Free;
  end;
end;

procedure TDBImportDataForm.GetSourceDBConnSBtnClick(Sender: TObject);
var theTables: TStringList;
begin
  SourceDBConnEd.Text:='';
  DMDB.DisconnectFromDB;

  //do until a successful connection is established or the user selects abort
  while(1=1)do
  begin
    //Let the User choose connection
    SourceDBConn:=DMDB.GetUserSelectedDBConn('');
    if(SourceDBConn<>nil)then
    begin
      //Try to connect to the DB
      try
        DMDB.ConnectToDB(SourceDBConn, SourceSQLConn);
      except
        on x: Exception do
        begin
          MessageDlg('Connection to database failed.'+#13#10#13#10+
            x.Message, mtError, [mbOK], 0);

          continue;
        end;
      end;

      SourceDBConnEd.Text:=SourceDBConn.Name;

      SourceTblsLBox.Items.Clear;

      theTables:=TStringList.Create;
      try
        DMDB.GetDBTables(theTables, SourceSQLConn, SourceDBConn);

        //Ignore DBDesigner4 table
        if(theTables.IndexOf('DBDesigner4')<>-1)then
          theTables.Delete(theTables.IndexOf('DBDesigner4'));

        SourceTblsLBox.Items.Assign(theTables);
      finally
        theTables.Free;
      end;

      break;
    end
    else
      break;
  end;
end;

procedure TDBImportDataForm.DirEdKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if(key=Key_Return)or(key=Key_Enter)then
    RefreshFileList;
end;

procedure TDBImportDataForm.SourceLBoxClick(Sender: TObject);
var theTblOpt: PTableImportOptions;
begin
  if(SourceLBox.ItemIndex<0)then
    Exit;

  //when a table is selected and checked, show it's options
  if(SourceLBox.ItemIndex<>OptionsForTable)and(SourceLBox.Checked[SourceLBox.ItemIndex])then
  begin
    OptionsForTable:=SourceLBox.ItemIndex;

    ShowTableOptions(OptionsForTable);
  end
  else if(Not(SourceLBox.Checked[SourceLBox.ItemIndex]))then
  begin
    theTblOpt:=GetTblOpt(SourceLBox.ItemIndex);

    //If this table has ImportOptions delete them
    if(theTblOpt<>nil)then
    begin
      TblOptList.Delete(TblOptList.IndexOf(theTblOpt));

      theTblOpt.TextColumns.Free;
      theTblOpt.TextTestLines.Free;
      theTblOpt.ColumnNames.Free;
      theTblOpt.DestColumns.Free;
      theTblOpt.DestColumnsValues.Free;
      theTblOpt.ReplaceChars.Free;

      dispose(theTblOpt);

      ActiveTableOptions:=nil;
    end;

  end;
end;

procedure TDBImportDataForm.ShowTableOptions(TblIndex: integer);
var theTblOpt: PTableImportOptions;
  theFile: TextFile;
  i, j, maxlen: integer;
  s: string;
begin
  if(ModePageControl.ActivePage=TextImportSheet)then
  begin
    //File mode
    theTblOpt:=GetTblOpt(TblIndex);

    //If this table has no ImportOptions yet, create them
    if(theTblOpt=nil)then
    begin
      new(theTblOpt);
      theTblOpt.Tablename:=SourceLBox.Items[TblIndex];
      theTblOpt.TableDirectory:=SourceDir;
      theTblOpt.Tableindex:=TblIndex;
      theTblOpt.TextImportWithSep:=True;
      theTblOpt.TextImportFirstRowHoldsColumnNames:=True;
      theTblOpt.TextImportTextDelim:='"';
      theTblOpt.TextImportTextSep:=';';
      theTblOpt.TextColumns:=TStringList.Create;
      theTblOpt.TextColumns.Add('1');
      theTblOpt.TextTestLines:=TStringList.Create;
      theTblOpt.ColumnNames:=TStringList.Create;
      theTblOpt.DestTableName:='';
      theTblOpt.DestColumns:=TStringList.Create;
      theTblOpt.DestColumnsValues:=TStringList.Create;
      theTblOpt.DelDestTableBeforInserts:=False;
      theTblOpt.TrimColumns:=True;
      theTblOpt.ReplaceChars:=TStringList.Create;
      theTblOpt.CheckReturnsInTxt:=True;
      theTblOpt.PresetName:='';

      //Get TestLines
      AssignFile(theFile, SourceDir+theTblOpt.Tablename);
      Reset(theFile);
      try
        i:=0;
        while(Not(EOF(theFile)))and(i<20)do
        begin
          ReadLn(theFile, s);

          if(Trim(s)='')then
            continue;

          theTblOpt.TextTestLines.Add(s);
          inc(i);
        end;
      finally
        CloseFile(theFile);
      end;


      TblOptList.Add(theTblOpt);

      //Get DestTable columns
      ActiveTableOptions:=theTblOpt;
      DestTblLUCloseUp(self);
    end;

    //Show Options of selected Table
    ActiveTableOptions:=theTblOpt;

    IsSettingOptions:=True;

    TextWithSepRBtn.Checked:=theTblOpt.TextImportWithSep;
    SepOptionsGBox.Visible:=theTblOpt.TextImportWithSep;

    TextWithFixedLengthRBtn.Checked:=Not(theTblOpt.TextImportWithSep);
    FixLengthOptionsGBox.Visible:=Not(theTblOpt.TextImportWithSep);

    FixedLengthHintLbl.Visible:=TextWithFixedLengthRBtn.Checked;

    FirstRowHoldsColNameCBox.Checked:=theTblOpt.TextImportFirstRowHoldsColumnNames;
    IgnoreFirstLineCBox.Checked:=theTblOpt.TextImportFirstRowHoldsColumnNames;

    if(theTblOpt.TextImportTextDelim='"')then
      TextDelimLU.ItemIndex:=0
    else if(theTblOpt.TextImportTextDelim='''')then
      TextDelimLU.ItemIndex:=1
    else
      TextDelimLU.ItemIndex:=2;

    if(theTblOpt.TextImportTextSep='_tab')then
      SepTabRBtn.Checked:=True
    else if(theTblOpt.TextImportTextSep=';')then
      SepSemicolonRBtn.Checked:=True
    else if(theTblOpt.TextImportTextSep=',')then
      SepCommaRBtn.Checked:=True
    else if(theTblOpt.TextImportTextSep=' ')then
      SepSpaceRBtn.Checked:=True
    else
    begin
      SepUserDefRBtn.Checked:=True;
      SepUserDefEd.Text:=theTblOpt.TextImportTextSep;
    end;

    s:='';

    //Check if all values are integers
    i:=0;
    while(i<theTblOpt.TextColumns.Count)do
    begin
      try
        StrToInt(theTblOpt.TextColumns[i]);
        inc(i);
      except
        theTblOpt.TextColumns.Delete(i);
      end;
    end;

    IsSettingOptions:=False;

    TextGrid.RowCount:=theTblOpt.TextTestLines.Count;

    //Preview Grid mit TestLines auff黮len
    if(Not(theTblOpt.TextImportWithSep))then
    begin
      //get max length
      maxlen:=0;
      for i:=0 to theTblOpt.TextTestLines.Count-1 do
        if(maxlen<Length(theTblOpt.TextTestLines[i]))then
          maxlen:=Length(theTblOpt.TextTestLines[i]);

      TextGrid.ColCount:=maxlen;
      TextGrid.DefaultColWidth:=10;


      for i:=0 to theTblOpt.TextTestLines.Count-1 do
        for j:=0 to Length(theTblOpt.TextTestLines[i])-1 do
          TextGrid.Cells[j, i]:=theTblOpt.TextTestLines[i][j+1];

      //sicherstellen, da

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -