Autor Beitrag
herrfuchs
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Di 01.01.13 17:07 
Hallo!

Ich bräuchte die aktuelle ID des erstellten Eintrags. Access Datenbank

Neuer Eintrag in der Datenbank:
ausblenden C#-Quelltext
1:
testTableAdapter.Insert(120,150,120);					


von diesem Eintrag brauch ich jetzt die zugewiesene ID gibt es da irgendeinen Befehl die ID abzurufen oder muss man das mit einer manuellen SQL Abrage machen?

Gutes neues Jahr!
Mfg herrfuchs
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Di 01.01.13 17:15 
Hallo und Frohes Neues Jahr.

Stichwort: @@identity, s. z.B. HOW TO: Retrieve the Identity Value While Inserting Records into Access Database By Using Visual Basic .NET (läßt sich aber analog in C# übernehmen)
herrfuchs Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Di 01.01.13 18:05 
Danke
Hätte es jetzt so umgesetzt, es kommt zwar kein Fehler aber es wird auch keinen ID ermittelt:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
 string strSQL = "SELECT @@IDENTITY FROM tabelle";

                OleDbCommand cmd = new OleDbCommand(strSQL, con);
                cmd.ExecuteNonQuery();
                wert = (int)cmd.ExecuteScalar();



                con.Close();
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 01.01.13 18:46 
Wo hast du das mit dem '... from Table' her? Laut Doku ist es einfach 'select @@identity'.

Und noch ein Zitat aus der Hilfe das dir vielleicht hilft.

Zitat:
The @@IDENTITY variable listed in the previous SQL statements can be executed only through the Jet OLE DB provider and ADO; it will result in a value of 0 if used through the Access SQL View user interface. In addition, the variable is set only when records are inserted through programming code. If a record is inserted through the user interface, either with datasheets, forms, or SQL statements in the Access SQL View window, the @@IDENTITY variable will return 0. For this reason, the value of @@IDENTITY is only accurate immediately after adding a record from code


Du musst also in der selben Session sein wie der Insert um ein Ergebnis zu bekommen. Zum Beispiel darfst du die Connection zwischenzeitlich nicht schließen.
herrfuchs Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Di 01.01.13 20:39 
Okay
Indem ich die Datenbank mit dem tableAdapter fülle:
testTableAdapter.Insert(120,150,120);

wäre es da nicht einfacher, wenn ich die Datenbank mit der größten ID durchsuche?
Ansonst müsste ich die Werte auch manuell über SQL Oledb hinzufügen.

Edit:
Habe es jetzt mit der Suche nach der MAX ID gelöst:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
string strSQL = "SELECT MAX(id) FROM table";

                OleDbCommand cmd = new OleDbCommand(strSQL, con);
                cmd.ExecuteNonQuery();
                wert = (int)cmd.ExecuteScalar();
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Di 01.01.13 21:03 
Das ist nur in einer reinen Single User Umgebung zu empfehlen wenn überhaupt.

Sieh dir doch einfach den Code der Insert Methode mal an. Der liegt im Klartext in deiner Solution rum. Dort wirst du sehen das dort die Connection nur geöffnet und anschließend geschlossen wird die nicht schon vorher offen war. Du solltest also manuell die Connection vor dem Aufruf von Insert öffnen, danach den Select @@Identity auszuführen und anschließend die Connection auch wieder manuell schließen.