Autor Beitrag
iwb-augsburg
Hält's aus hier
Beiträge: 6



BeitragVerfasst: Mo 19.11.07 14:04 
ACHTUNG: das ganze Programm ist dem Beitrag hinzugefügt worden, also braucht Ihr die CODE-Azszüge nicht unbedingt zu lesen
Hallo,
leider konnte ich an meiner Hochschule keinen Professor finden, der mir mit meinem Problem in C# helfen könnte. Leider arbeite ich selber erst seit wenigen Tagen mit C# und komme daher alleine nicht weiter.
Ich habe ein kleines Programm geschrieben, welches zu einer ACCESS-Datenbank eine Verbindung aufbaut und dort daraufhin einige Werte ändert.
Leider bekomme ich immer wieder die Meldung:

Die 'ConnectionString'-Eigenschaft darf nicht geändert werden.Der aktuelle Status der Verbindung ist 'Geöffnet'.

Das Problem tritt in der Zeile 47 bei CoDeFSmain.cs, da es in der Klasse DB_Connect.cs in Zeile 44 verursacht wird.


Codeauszüge:



CoDeFSmain.cs
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:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;                                                           
using System.Data.SqlClient;                                                        
using System.Drawing;
using System.Text;
using System.Windows.Forms;                                                         

namespace CoDeFS
{
    public partial class CoDeFSmain : Form
    {
        DB_Connect connect_db = new DB_Connect();
        Start_CoDeFS startcodefs = new Start_CoDeFS();
        public CoDeFSmain()
        {
            InitializeComponent();
        }



        // Diese Methode wird beim Starten geladen. 
        private void Load_OleDBConnection_on_start (object sender, EventArgs e)
        {
            connect_db.con = new OleDbConnection();
            connect_db.Checkdbcon = false;                                     
        }



        // ANFANG: BLOCK - DATENBANKVERBINDUNG



        // Tastenfunktion für DB-Connection: Taste - Db_connection_btn
        private void connect_to_db_Click(object sender, EventArgs e)
        {
            if (connect_db.Checkdbcon == false)                                  
            {
                Db_connection_btn.Text = "disconnect";
                /*GEÄNDERT
                connect_db.dbconnect();
                startcodefs.start_codefs();
                */

                OleDbConnection con = connect_db.DBConnect;
                startcodefs.start_codefs(con);
            }
            else
            {
                Db_connection_btn.Text = "connect";
                connect_db.dbdisconnect();
            }
        }

        // ENDE: BLOCK - DATENBANKVERBINDUNG
    }
}





DB_Connect.cs
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:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
using System;                                                                       
using System.Collections.Generic;                                                   
using System.Data.OleDb;                                                            
using System.Data.SqlClient;                                                        
using System.Text;                                                                  
using System.Windows.Forms;                                                         

namespace CoDeFS
{
    class DB_Connect
    {
        // Variablen
        public OleDbConnection con = new OleDbConnection();                       
        private string pfad_codefsinterface = "Data Source=C:\\Projects\\Cogfacinterface.mdb";
        private bool checkcon;


        // ANFANG: BLOCK - DATENBANKVERBINDUNG   

        
        public OleDbConnection DBConnect
        {
            get
            {
                con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
                con.ConnectionString += pfad_codefsinterface;                           // Pfad für die Datenbank

                try
                {
                    con.Open();
                    checkcon = true;
                    return con;
                }
                catch (OleDbException er)
                {
                    MessageBox.Show(er.Message);
                }
                return null;
            }
            //Die Verbindung setzen
            set
            {
                con = value;
            }
        }



        // Löst die Verbindung zur Datenbank
        public void dbdisconnect()
        {
            try
            {
                con.Close();
            }
            catch (OleDbException er)
            {
                MessageBox.Show(er.Message);                                            // Fehlermeldung z.B. bei fehlender Datenbank
            }
                        
            checkcon = false;
        }

        // ENDE: BLOCK - DATENBANKVERBINDUNG



        
        public bool Checkdbcon
        {
            get
            {
                return this.checkcon;                                                                                                    
            }
            set
            {
                this.checkcon = value;                                         
            }
        }
    }
}





Start_CoDeFS.cs
ausblenden 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:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace CoDeFS
{
    class Start_CoDeFS
    {
        // Variablen
        DB_Connect checkconnection = new DB_Connect();
        Application_State set_state = new Application_State();
        private string applicationstate;




        // ANFANG: BLOCK - DATENBANKABFRAGE
       

        public void start_codefs(OleDbConnection con)
        {
            applicationstate = "start";
            set_state.set_application_state(applicationstate,con);
        }
    }
}





Application_State.cs
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:
using System;
using System.Collections.Generic;
using System.Data.OleDb;                                                            
using System.Data.SqlClient;                                                        

using System.Text;

namespace CoDeFS
{
    class Application_State
    {
        // Variablen
        DB_Connect connect = new DB_Connect();
        private int iRowsAffeced;
        private string set_state_ready = "UPDATE [cosimirinterface] SET spalte2 = 'ready' WHERE [spalte1] = 'CoDeFS'";
        private string set_state_not_ready = "UPDATE [Application_State] SET application_state = 'not ready' WHERE [application_name] = 'CoDeFS'";
        



        // Setzt "application_state" in der Tabelle "Application_State", in der Zeile "CoDeFS" auf "ready" oder "not ready"
        

        public void set_application_state(string state, OleDbConnection con) 
        {
            if (state == "start")
            {
                state = set_state_ready;
            }
            else 
            {
                state = set_state_not_ready;
            }

            OleDbCommand stateUpdCmd = new OleDbCommand(set_state_ready, con);
            iRowsAffeced = stateUpdCmd.ExecuteNonQuery();
        }
    }
}


Moderiert von user profile iconChristian S.: Quote- durch C#-Tags ersetzt
Moderiert von user profile iconChristian S.: Topic aus C# - Die Sprache verschoben am Mo 19.11.2007 um 13:58
Einloggen, um Attachments anzusehen!
iwb-augsburg Threadstarter
Hält's aus hier
Beiträge: 6



BeitragVerfasst: Mo 19.11.07 16:58 
kann geschloßen werden
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mo 19.11.07 17:08 
Wenn Du eine Lösung hast, dann schreibe Sie bitte in den Thread, damit auch andere etwas davon haben. Danke! :-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".