Warten bis Update-Befehl fertig ist

  • C#

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von Mono.

    Warten bis Update-Befehl fertig ist

    Hallo,

    habe gerade ein Problem mit der Software an der ich gerade arbeite.
    Ich habe mir eine Database Klasse geschrieben die CRUD-Vorgänge für alle anderen Klassen durchführt.
    Nun habe ich die Klasse Autos, diese hat eine update methode die ein Dictionary erwartet (<spalte>, <neuer_wert>), diese methode übergibt wiederum der Database.Update Methode dieses
    Dictionary + ein weiteres (<where>, <wert>).

    Anschließend gibt die Auto.update() eine neue Instanz zurück die mittels getAutoByID geholt wird zurück, aber ich bekomme noch die alten Daten zurück, daher muss ich irgendwie warten bis der Databank-Update-Befehl fertig ist.
    Gibts da eine Möglichkeit?

    (bitte keine tipps wie du solltest db-parameter verwenden usw... das werde ich natürlich alles noch berücksichtigen - es geht mir erstmal nur um das oben genannte problem)

    C#-Quellcode

    1. public Auto update(Dictionary<string, string> data)
    2. {
    3. Dictionary<string, object> where = new Dictionary<string, object>();
    4. where.Add("ID", this.ID);
    5. if (Database.UpdateData(data, where, "tbl_autos"))
    6. {
    7. return Auto.getAutoByID(this.ID);
    8. }
    9. return null;
    10. }



    C#-Quellcode

    1. public static void UpdateData(Dictionary<string, string> data, Dictionary<string, object> where, string table)
    2. {
    3. string query = "UPDATE " + table + " SET ";
    4. int index = 0;
    5. foreach (KeyValuePair<string, string> entry in data)
    6. {
    7. index++;
    8. if (index == data.Count)
    9. {
    10. query += entry.Key + " = '" + entry.Value + "'";
    11. }
    12. else
    13. {
    14. query += entry.Key + " = '" + entry.Value + "', ";
    15. }
    16. }
    17. query += " WHERE ";
    18. index = 0;
    19. foreach (KeyValuePair<string, object> entry in where)
    20. {
    21. index++;
    22. if (index == where.Count)
    23. {
    24. if (entry.Value is int)
    25. {
    26. query += entry.Key + " = " + entry.Value.ToString();
    27. } else
    28. {
    29. query += entry.Key + " = '" + entry.Value.ToString() + "'";
    30. }
    31. }
    32. else
    33. {
    34. if (entry.Value is int)
    35. {
    36. query += entry.Key + " = " + entry.Value.ToString() + " AND ";
    37. }
    38. else {
    39. query += entry.Key + " = '" + entry.Value.ToString() + "' AND ";
    40. }
    41. }
    42. }
    43. try
    44. {
    45. OleDbConnection conn = new OleDbConnection(connectionString);
    46. OleDbCommand cmd = new OleDbCommand(query, conn);
    47. conn.Open();
    48. cmd.ExecuteNonQuery();
    49. }
    50. catch (Exception ex)
    51. {
    52. MessageBox.Show(ex.ToString());
    53. }
    54. }

    Ohne inhaltlich auf iwas einzugehen.

    Der Datenbank Updatebefehl ist fertig, wenn cmd.ExecuteNonQuery() fertig ist.
    Die Funktion liefert btw auch einen Wert zurück. Bei Update die Anzahl der betroffenen Rows.
    Dein Problem liegt also woanders.
    (Vermutlich in return Auto.getAutoByID(this.ID);)
    LG
    Das ist meine Signatur und sie wird wunderbar sein!