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)
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
- public static void UpdateData(Dictionary<string, string> data, Dictionary<string, object> where, string table)
- {
- string query = "UPDATE " + table + " SET ";
- int index = 0;
- foreach (KeyValuePair<string, string> entry in data)
- {
- index++;
- if (index == data.Count)
- {
- query += entry.Key + " = '" + entry.Value + "'";
- }
- else
- {
- query += entry.Key + " = '" + entry.Value + "', ";
- }
- }
- query += " WHERE ";
- index = 0;
- foreach (KeyValuePair<string, object> entry in where)
- {
- index++;
- if (index == where.Count)
- {
- if (entry.Value is int)
- {
- query += entry.Key + " = " + entry.Value.ToString();
- } else
- {
- query += entry.Key + " = '" + entry.Value.ToString() + "'";
- }
- }
- else
- {
- if (entry.Value is int)
- {
- query += entry.Key + " = " + entry.Value.ToString() + " AND ";
- }
- else {
- query += entry.Key + " = '" + entry.Value.ToString() + "' AND ";
- }
- }
- }
- try
- {
- OleDbConnection conn = new OleDbConnection(connectionString);
- OleDbCommand cmd = new OleDbCommand(query, conn);
- conn.Open();
- cmd.ExecuteNonQuery();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.ToString());
- }
- }