Autor Beitrag
mo0n_
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 50

Win7
C#
BeitragVerfasst: Do 08.03.12 12:46 
Hallo liebes Forum, me again,

Ich möchte ein Update in meiner Datenbank durchführen, welches auch an sich schon funktioniert, allerdings nach neustarten meines Programms wieder weg ist.

Hier mein Quellcode:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
       string settings_set = null;
            string settings_unset = null;

            String LoginConnectionString = ".......................................................................";
            OleDbConnection Connection = new OleDbConnection(LoginConnectionString);

            switch (Berechtigungen.SelectedIndex)
            {
                case 0:
                settings_set = "UPDATE Berechtigungen SET Active = 1 WHERE ID = 1;";  
                settings_unset = "UPDATE Berechtigungen SET Active = 0 WHERE ID = 2 or ID = 3 or ID = 4;"break;
                
             case 1:
                    
                 settings_set = "UPDATE Berechtigungen SET Active = 1 WHERE ID = 2;";
                 settings_unset="UPDATE Berechtigungen SET Active = 0 WHERE ID = 1 or ID = 3 or ID = 4;";break;
                    
             case 2:
                    
                settings_set = "UPDATE Berechtigungen SET Active = 1 WHERE ID = 3;";
                settings_unset="UPDATE Berechtigungen SET Active = 0 WHERE ID = 2 or ID = 1 or ID = 4;";break;
                    
             case 3:
                    
                 settings_set = "UPDATE Berechtigungen SET Active = 1 WHERE ID = 4;";
                 settings_unset="UPDATE Berechtigungen SET Active = 0 WHERE ID = 2 or ID = 3 or ID = 1;";break;   
                
         }
         OleDbCommand SetCommand = new OleDbCommand(settings_set);
         OleDbCommand UnsetCommand = new OleDbCommand(settings_unset);

         SetCommand.Connection = Connection;
         Connection.Open();
         SetCommand.ExecuteNonQuery();
         SetCommand.Connection.Close();

         UnsetCommand.Connection = Connection;
         Connection.Open();
         UnsetCommand.ExecuteNonQuery();
         UnsetCommand.Connection.Close();
                

         Close();


Vielen Dank für eure Hilfen,
mo0n_

_________________
the cake is a lie
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: Do 08.03.12 13:22 
Hallo mo0n_,

sofern keine Exception kommt sind die Daten auch in der DB gespeichert.
Kann es aber sein, daß du eine lokale Datenbank benutzt und diese bei einem Build immer wieder neu in das Ausgabeverzeichnis kopiert wird (Einstellung "Copy to Output Dir": "copy always" -> "copy if newer" bzw. "do not copy")?

P.S. Deine SQL-Commands könnte man noch verkürzen (so daß kein switch mehr benötigt wird) zu:
ausblenden SQL-Anweisung
1:
2:
UPDATE Berechtigungen SET Active = 0 WHERE ID <> :ID
UPDATE Berechtigungen SET Active = 1 WHERE ID = :ID

wobei ":ID" dann dein Wert wäre (s.a. SQL: Parameter von Befehlen).
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: Do 08.03.12 13:57 
Es würde helfen zu wissen was du für eine Datenbank verwendest. Dann läßt sich auch ein einfacheres Sql empfehlen das deinen Code deutlich entschlacken sollte.
Gefühlt fehlt mir da eine Transaktionsteuerung in deinem Code.

Im SqlServer könnte man das zum Beispiel so durch ein einziges Sql erschlagen.

ausblenden SQL-Anweisung
1:
UPDATE Berechtigungen SET Active = CASE WHEN ID = :ID THEN 1 ELSE 0 END;					
mo0n_ Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 50

Win7
C#
BeitragVerfasst: Do 08.03.12 14:59 
Danke für eure Hilfe.

Zur Klärung:
Ich verwende eine OleDB Datenbank.
Diese ist lokal gespeichert.

Allerdings hat sich mein Problem dank TH69 schon erledigt, denn nun klappt alles wunderbar :)
:dance2:

_________________
the cake is a lie
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: Do 08.03.12 15:57 
Zitat:
Ich verwende eine OleDB Datenbank.


:gruebel: OleDb ist eine Datenbank Zugriffstechnik aber selbst keine Datenbank.
mo0n_ Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 50

Win7
C#
BeitragVerfasst: Fr 09.03.12 00:02 
Access

_________________
the cake is a lie