Einzelnen Beitrag anzeigen

Moony

Registriert seit: 29. Jul 2003
Ort: Essen
454 Beiträge
 
Delphi 2010 Professional
 
#10

Re: ALTER TABLE CHANGE - Spaltennamen ändern in Access

  Alt 30. Jan 2009, 10:05
So, habe jetzt alles fertig, aber bekomme beim Verlassen der Routine eine Access Violation.

So sieht alles bei mir aus:

Delphi-Quellcode:
const
  PHOTOTABLE = 'Tabellenname';

...
if (UpperCase(PhotoTB.FieldList.CommaText) = UpperCase(ImageSet.DefLst.CommaText)) and
   (PhotoTB.FieldCount = ImageSet.DefLst.Count) then
begin
  // alte Tabelle umbenennen
  PhotoTB.Close;
  cat := CoCatalog.Create;
  try
    cat.Set_ActiveConnection(PhotoDB.ConnectionObject);
    cat.Tables.Item[PHOTOTABLE].Name := '_images';
    cat._Release;
  except
    Result := false;
    Exit;
  end;

  // Neue Tabelle erstellen
  SQL := 'CREATE TABLE ' + PHOTOTABLE + ' (';
  if Length(_myLen) >= ImageSet.DefLst.Count then
  begin
    for i := 0 to ImageSet.DefLst.Count - 1 do
      if ImageSet.DefLst.Strings[i] <> 'Filenamethen
        SQL := SQL + ' Field' + ImageSet.DefLst.Strings[i] + ' CHAR(' + IntToStr(_myLen[i]) + '),';

      SQL := SQL + 'FileName VARCHAR(' + IntToStr(_myLen[ImageSet.DefLst.Count-1]) + '))';
      PhotoCmd.CommandText := SQL;
      PhotoCmd.Execute;
  end
  else begin
    Result := false;
    Exit;
  end;

  // Daten kopieren
  myDS := TADODataSet.Create(nil);
  try
    myDS.Connection := PhotoDB;
    myDS.CommandText := 'SELECT * FROM _' + PHOTOTABLE;
    myDS.Open;
    myDS.First;

    PhotoTB.CommandText := 'SELECT * FROM ' + PHOTOTABLE;
    PhotoTB.Open;
    PhotoTB.First;
    while not myDS.Eof do
    begin
      PhotoTB.Append;
      for i := 0 to myDS.FieldCount - 1 do
      begin
        if UpperCase(myDS.Fields.Fields[i].FieldName) <> UpperCase('FileName') then
     PhotoTB.FieldByName('Field' + myDS.Fields.Fields[i].FieldName).Text := myDS.Fields.Fields[i].AsString
        else PhotoTB.FieldByName(myDS.Fields.Fields[i].FieldName).Text := myDS.Fields.Fields[i].AsString;
      end;
      try
        PhotoTB.Post;
        myDS.Next;
      except
        PhotoTB.Cancel;
        Result := false;
        Exit;
      end;
    end;

    myDS.Close;
    myDS.Free;
    PhotoTB.Close;
    PhotoDB.Close;
  except
    myDS.Free;
    Result := false;
    Exit;
  end;

  // alte Tabelle löschen
  PhotoCmd.CommandText := 'DROP TABLE _' + PHOTOTABLE;
  PhotoCmd.Execute();
  Application.ProcessMessages;
end;

if not PhotoDB.Connected then
  PhotoDB.Open;
if not PhotoTB.Active then
  PhotoTB.Open;

PhotoTB.Active := True;
Exit; <- an dieser Stelle bekomme ich die Access Violation
...
Die einzelnen Operationen verlaufen alle richtig. Tabelle umbenennen, neue Tabelle erstellen, Daten kopieren und die alte löschen. Aber ich kann hier nicht raus.

Hoff ihr wisst weiter....
Ich weiß, daß ich nichts weiß! Sokrates
  Mit Zitat antworten Zitat