Autor Beitrag
dirkil2
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130



BeitragVerfasst: Do 31.08.06 12:28 
Ich möchte aus einer Tabelle einen Record löschen, dessen Primary Key eine Guid ist. Den Guid selber hole ich aus einer anderen Tabelle. Das Problem ist, daß der Eintrag nicht gelöscht wird.

Ich hole mir die Guid mit:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
var
  lGuid: TGUID;
...
lGuid := TGuidField(FormSet.FieldByName('xxId')).AsGuid;


Und löschen möchte ich sie damit:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
  lDelSql := 'DELETE FROM xxx WHERE myId = :myId';
  with lCmd do
    begin
      CommandText := lDelSql;
      CommandType := cmdText;
      Parameters.ParamByName('myId').Value := GuidToString(lGuid);
      Execute;
    end;

Wenn ich mir GuidToString(lGuid) anzeigen lasse, dann sehe ich links und rechts jeweils noch eine { und } . Wenn ich ein SELECT gegen die Datenbank (SQL Server 2000) mache, dann werden mir die { } nicht angezeigt. Der Datentyp in der Datenbank ist uniqueidentifier.

Was mache ich falsch?
iKilledKenny
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 8

Win XP
D5 Prof, C# Express 2005
BeitragVerfasst: Do 31.08.06 13:58 
versuchs mal mit

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
  with lCmd do
    begin
      CommandText := lDelSql;
      CommandType := cmdText;
      with Parameters.ParamByName('myId') do
        begin
          DataType := ftGuid;
          Value := lGuid;
        end;
      Execute;
    end;


ist ungetestet, sollte aber gehen.

grüße
alex
dirkil2 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130



BeitragVerfasst: Do 31.08.06 14:48 
Das compiliert schon gar nicht:

Bei der Zuweisung Value := lGuid; kommt folgende Compilermeldung:

[Fehler]: Inkompatible Typen: 'Variant' und 'TGUID'
iKilledKenny
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 394
Erhaltene Danke: 8

Win XP
D5 Prof, C# Express 2005
BeitragVerfasst: Do 31.08.06 20:21 
habs nun bei mir mit sql server express probiert, und so gehts:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
procedure TForm1.Button1Click(Sender: TObject);

var   g : TGUID;

begin
  CoCreateGUID (g);

  ADOConnection1.Connected := True;

  ADOCommand1.CommandText := 'insert into Table_1 values (:ID)';
  ADOCommand1.ParamCheck := True;
  ADOCommand1.Parameters [0].DataType := ftGuid;
  ADOCommand1.Parameters [0].Value := GUIDToString (g);
  ADOCommand1.Execute;

  ADOCommand1.CommandText := 'delete Table_1 where ID = :ID';
  ADOCommand1.ParamCheck := True;
  ADOCommand1.Parameters [0].DataType := ftGuid;
  ADOCommand1.Parameters [0].Value := GUIDToString (g);
  ADOCommand1.Execute;
end;


das macht erst einen insert und dann ein delete der eben geinserteten guid.

grüße
alex
dirkil2 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 130



BeitragVerfasst: Do 31.08.06 23:55 
@iKilledKenny und andere

Ich hab's jetzt hinbekommen, aber es lag nicht am GUID. Dein Code funktioniert natürlich einwandfrei und meiner hat es auch schon getan. Das Problem war, daß ich den SELECT aus einer anderen Datenbank gemacht habe als das DELETE. Dann kann der Record natürlich nicht gelöscht werden. Zu meiner Verteidigung kann ich noch sagen, daß die Datenbanken sehr ähnliche Namen haben.

Typischer Fall von "Programmer-too-stupid-error". Auf jeden Fall ein großes Dank an alle, die versucht haben, mir zu helfen.