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
DB_Connect.cs
Start_CoDeFS.cs
Application_State.cs
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
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
}
}
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
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;
}
}
}
}
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
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,c on);
}
}
}
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,c on);
}
}
}
Application_State.cs
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();
}
}
}
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();
}
}
}
Comment