Hoi allen,
ik heb het volgende geschreven:
- DatabaseManager.cs
- NieuweGebruiker.cs:
- NieuweGebruiker_controller.cs:
- NieuweGebruikerView.cs:
DatabaseManager.cs:
NieuweGebruiker.cs:
KLASSE: NieuweGebruiker_controller.cs:
KLASSE: NieuweGebruikerView.cs:
Maar er wordt dus nog niks opgeslagen als je op de button “Toevoegen” drukt.
Ikzelf denk dat het in dit stukje code ligt, van de klasse: DatabaseManager:
Maar heb al van alles geprobeerd. Mischien kunnen jullie me helpen?
Alvast bedankt.
ik heb het volgende geschreven:
- DatabaseManager.cs
- NieuweGebruiker.cs:
- NieuweGebruiker_controller.cs:
- NieuweGebruikerView.cs:
DatabaseManager.cs:
Code:
using System;
using System.Collections;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using DatabaseApplication;
using System.Collections.Generic;
namespace DatabaseApplication
{
public class DatabaseManager
{
//DataRow drNewRow = dtDataTable.NewRow();
public DataTable m_dtkastCollectie = new DataTable();
public DataTable m_dtkast = new DataTable();
public DataTable m_dtgebruiker = new DataTable();
public DataTable m_dtkastSoort = new DataTable();
public DataTable m_dtboek = new DataTable();
private SqlDataReader reader;
private SqlCommand command;
private SqlConnection connection;
private string connectionString;
string dbPath = System.Configuration.ConfigurationSettings.AppSettings["c:\\MeetSysteem.mdb"];
//string dbPath = App.config.ConfigurationSettings.AppSettings["c:\\MeetSysteem.mdb"];
/// <summary>
/// An abstract class for managing a connection with and data to and from an instance of SQL Server.
//ConnectieString
private string GetConnectionString()
{
OleDbConnection aConnection = new OleDbConnection(GetConnectionString());
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\meetSysteem.mdb";
//naar config file eingelijk
return connectionString;
}
private object ReadValue(string selectStatement, string field)
{
object retVal = null;
//create the database connection
OleDbConnection aConnection = new OleDbConnection(GetConnectionString());
OleDbCommand aCommand;
OleDbDataReader aReader;
try
{
aCommand = new OleDbCommand(selectStatement, aConnection);
//selectStatement = "SELECT gebruiker.gebruikersnaam, Count(gebruiker.gebruikersnaam) AS CountOfemp_name FROM gebruiker GROUP BY gebruiker.gebruikersnaam";
aConnection.Open();
aReader = aCommand.ExecuteReader();
if (aReader.Read())
retVal = aReader[field];
aReader.Close();
aReader.Dispose();
}
catch (OleDbException se)
{
Console.WriteLine(se.Message);
}
catch (InvalidOperationException ioe)
{
if (ioe.Message.StartsWith("Timeout")) //If timeout, try again
return ReadValue(selectStatement, field);
else
System.Windows.Forms.MessageBox.Show(ioe.Message, "Database access error");
}
finally
{
aConnection.Close();
aConnection.Dispose();
}
return retVal;
}
public ArrayList Select(string table, string ordering)
{
return this.ExplicitQuery("SELECT * FROM " + table + " ORDER BY " + ordering);
}
//Functie toevoegen
public bool Toevoegen(string table, string[] schema, params object[] fields)
{
//OleDbDataAdapter daDataAdapter = new OleDbDataAdapter("SELECT * FROM gebruiker",
//"Provider=MicroSoft.JET.OLEDB.4.0; data source = C:\\meetSysteem.mdb");
NieuweGebruiker nieuweGebruiker = new NieuweGebruiker();
//Begin opslaan
DataRow drNewRow = m_dtgebruiker.NewRow();
DataTable dtgebruiker = new DataTable();
DataTable dtDataTable; //new DataTable();
//dtDataTable = dsDataSet.Tables[0];
//InsertKeyMode.Insert("gebruikersnaam");
//schema.SetValue("gebruikersnaam", "email", "wachtwoord" );
table.Contains("gebruiker");
fields.SetValue(1, 2, 3);
drNewRow["gebruikersnaam"] = "";
drNewRow["email"] = "hallo";
drNewRow["wachtwoord"] = "mooi";
drNewRow["wachtwoord"] = "mooi";
m_dtgebruiker.Rows.Add(drNewRow);
for (int i = 1; i < fields.Length; i++)
{
Console.Write(fields[i] + "");
}
Console.WriteLine();
//int[] myarray = new int[3] { 10, 11, 12 };
//Toevoegen(myarray);
if (schema.Length != fields.Length)
throw new ArgumentException("The number of fields does not match the size of the schema.");
string query = "INSERT INTO " + table + " (";
for (int i = 0; i < schema.Length; i++)
{
query += schema[i];
if (i != schema.Length - 1)
query += ",";
}
query += ") VALUES (";
for (int i = 0; i < fields.Length; i++)
{
query += Formatted(fields[i]);
if (i != fields.Length - 1)
query += ",";
}
query += ")";
return this.ExplicitNonQuery(query);
}
//Functie verwijderen
public bool Verwijderen(string table, string[] keys, params object[] fields)
{
if (keys.Length != fields.Length)
throw new ArgumentException("The number of fields does not match the number of keys.");
string query = "DELETE FROM " + table + " WHERE ";
for (int i = 0; i < fields.Length; i++)
{
query += keys[i] + " = " + Formatted(fields[i]);
if (i != fields.Length - 1)
query += " AND ";
}
return this.ExplicitNonQuery(query);
}
//Functie data updaten
public bool Update(string table, string[] schema, int[] keys, params object[] fields)
{
if (schema.Length != fields.Length)
throw new ArgumentException("The number of fields does not match the size of the schema.");
string query = "UPDATE " + table + " SET ";
for (int i = 0; i < fields.Length; i++)
{
bool is_key = false;
for (int j = 0; j < keys.Length; j++)
if (keys[j] == i)
is_key = true;
if (!is_key)
{
query += schema[i] + " = " + Formatted(fields[i]);
if (i != fields.Length - 1)
query += ",";
}
}
query += " WHERE ";
for (int i = 0; i < keys.Length; i++)
{
query += schema[keys[i]] + " = " + Formatted(fields[keys[i]]);
if (i != keys.Length - 1)
query += " AND ";
}
return this.ExplicitNonQuery(query);
}
public bool ExplicitNonQuery(string query)
{
try
{
this.command.CommandText = query;
return this.command.ExecuteNonQuery() > 0;
}
catch (SqlException ex)
{
return false;
}
}
procedure.</returns>
public ArrayList ExplicitQuery(string query)
{
try
{
ArrayList gebruikersnaam = new ArrayList();
this.command.CommandText = query;
this.reader = this.command.ExecuteReader();
while (this.reader.Read())
{
ArrayList a = new ArrayList();
for (int i = 0; i < this.reader.FieldCount; i++)
a.Add(this.reader[i]);
gebruikersnaam.Add(a);
}
this.reader.Close();
return gebruikersnaam;
}
catch (SqlException ex)
{
return new ArrayList();
}
}
}
protected string Formatted(object o)
{
return null;
}
}
}
NieuweGebruiker.cs:
Code:
using DatabaseApplication;
using System;
using System.Collections;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using DatabaseApplication;
using System.Collections.Generic;
namespace DatabaseApplication
{
public class NieuweGebruiker
{
private string id;
private string gebruikersnaam;
private string wachtwoord;
private string email;
public NieuweGebruiker()
{
}
public NieuweGebruiker( string id,string gebruikersnaam, string wachtwoord, string email)
{
this.id = id;
this.gebruikersnaam = gebruikersnaam;
this.wachtwoord = wachtwoord;
this.email = email;
}
public void setID( string value)
{
this.id = value;
}
public string getID()
{
return this.id;
}
public void setGebruikersnaam ( string value)
{
this.gebruikersnaam = value;
}
public string getGebruikersnaam()
{
return this.gebruikersnaam;
}
public string getWachtwoord()
{
return this.wachtwoord;
}
public void setWachtwoord(string value)
{
this.wachtwoord = value;
}
public string getEmail()
{
return this.email;
}
public void setEmail( string value )
{
this.email = value;
}
//public NieuweGebruiker () { };
}
}
KLASSE: NieuweGebruiker_controller.cs:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.IO;
//using DatabaseApplication;
using DatabaseApplication;
namespace DatabaseApplication
{
public static class NieuweGebruiker_controller
{
// Constructor
public static void databaseFunctie()
{
ArrayList gebruikersnaam = new ArrayList();
ArrayList wachtwoord = new ArrayList();
ArrayList email = new ArrayList();
}
public static void ToevoegenDatabaseManager( NieuweGebruiker gebruiker )
//public void toevoegenGebruiker()
{
DatabaseManager d = new DatabaseManager();
string table = "gebruiker";
string[] destring = new string[3];
destring[0] = "id";
destring[1] = "gebruikersnaam";
destring[2] = "wachtwoord";
destring[3] = "email";
string[] data = new string[3];
data[0] = "";
data[1] = gebruiker.getGebruikersnaam();
data[2] = gebruiker.getWachtwoord();
data[3] = gebruiker.getEmail();
d.Toevoegen(table, destring, data);
}
}
}
KLASSE: NieuweGebruikerView.cs:
Code:
//using DatabaseApplcication;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using DatabaseApplication;
namespace DatabaseApplication
{
public partial class NieuweGebruikerView : Form
{
DatabaseConfig databaseConfig = new DatabaseConfig();
int m_rowPosition = 0;
public NieuweGebruikerView()
{
InitializeComponent();
}
private void NieuweGebruiker_Load(object sender, EventArgs e)
{
DatabaseConfig databaseConfig = new DatabaseConfig();
}
private void NieuweGebruiker_FormClosing(object sender, System.EventArgs e)
{
}
private void ShowCurrentRecord()
{
private void button1_Click(object sender, EventArgs e)
{
//Hardcode gebruiker met gegevens wordt naar NieuweGebruiker_controller gestuurd.
//DatabaseManager databaseManager = new databaseManager();
//databaseManager.Toevoegen();
NieuweGebruiker temp = new NieuweGebruiker();
temp.setGebruikersnaam(txtGebruikersnaam.Text = "");
temp.setEmail(txtEmail.Text = "");
temp.setWachtwoord(txtWachtwoord.Text = "");
temp.setWachtwoord(txtWachtwoordNogmaals.Text = "");
temp.setID("");
txtGebruikersnaam.Text = "";
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void txtWachtwoord_TextChanged(object sender, EventArgs e)
{
}
private void txtWachtwoordNogmaals_TextChanged(object sender, EventArgs e)
{
}
private void txtEmail_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
//}
Maar er wordt dus nog niks opgeslagen als je op de button “Toevoegen” drukt.
Ikzelf denk dat het in dit stukje code ligt, van de klasse: DatabaseManager:
Code:
public bool Toevoegen(string table, string[] schema, params object[] fields)
{
NieuweGebruiker nieuweGebruiker = new NieuweGebruiker();
//Begin opslaan
DataRow drNewRow = m_dtgebruiker.NewRow();
DataTable dtgebruiker = new DataTable();
DataTable dtDataTable; //new DataTable();
//dtDataTable = dsDataSet.Tables[0];
//InsertKeyMode.Insert("gebruikersnaam");
//schema.SetValue("gebruikersnaam", "email", "wachtwoord" );
table.Contains("gebruiker");
fields.SetValue(1, 2, 3);
drNewRow["gebruikersnaam"] = "";
drNewRow["email"] = "hallo";
drNewRow["wachtwoord"] = "mooi";
drNewRow["wachtwoord"] = "mooi";
m_dtgebruiker.Rows.Add(drNewRow);
for (int i = 1; i < fields.Length; i++)
{
Console.Write(fields[i] + "");
}
Console.WriteLine();
if (schema.Length != fields.Length)
throw new ArgumentException("The number of fields does not match the size of the schema.");
string query = "INSERT INTO " + table + " (";
for (int i = 0; i < schema.Length; i++)
{
query += schema[i];
if (i != schema.Length - 1)
query += ",";
}
query += ") VALUES (";
for (int i = 0; i < fields.Length; i++)
{
query += Formatted(fields[i]);
if (i != fields.Length - 1)
query += ",";
}
query += ")";
return this.ExplicitNonQuery(query);
}
Maar heb al van alles geprobeerd. Mischien kunnen jullie me helpen?
Alvast bedankt.