Ik heb een abstracte classe:
Maar hoe ik nu bijvoorbeeld de methode: Toevoegen gebruiken in een formulier met drie textvelden: 'gebruikersnaam' , ' email' en ' wachtwoord' . En dan is er een knop opslaan die ervoor zorgt dat het in de database komt te staan?
Alstublieft. Is er iemand die mij wat tips kan geven?
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;
using System.Data.Sql;
namespace DatabaseApplication
{
/// De klasse is abstract, gezien de methoden: Toevoegen, update en verwijderen voor algemeen gebruik zijn, dit
/// wil zeggen dat meerdere klassen van deze methoden gebruik maken.
public abstract class DatabaseManager
{
//DataRow drNewRow = dtDataTable.NewRow();
Gebruiker gebruiker = new Gebruiker();
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();
OleDbConnection conn = null;// Zorgt ervoor dat er een connectie tot stand kan worden gebracht.
OleDbDataReader reader = null; //
OleDbCommand command = null; //Zorgt ervoor dat Queries kunnen worden uitgevoerd.
OleDbDataAdapter dataAdapter;
//private SqlDataReader reader;
//private SqlCommand command;
//private SqlConnection connection;
private string connectionString;
int m_rowPosition = 0;
//ConnectieString
public string GetConnectionString()
{
string connString = @"
Provider = Microsoft.Jet.OLEDB.4.0;
Data Source = C:\meetSysteem.mdb";
return connString;
//create the database connection
OleDbConnection conn = new OleDbConnection(GetConnectionString());
OleDbCommand aCommand;
OleDbDataReader aReader;
try
{
conn = new OleDbConnection();
conn.Open();
//OleDbCommand cmd = new OleDbCommand(conn);
}
catch (Exception e)
{
Console.WriteLine("Error:" + e);
//System.Windows.Forms.MessageBox.Show(e.Message, "Database access error");
}
finally
{
conn.Close();
reader.Close();
}
}
//Einde verbinding met database
public ArrayList Select(string table, string ordering)
{
return this.ExplicitQuery("SELECT * FROM " + table + " ORDER BY " + ordering);
}
//Methode Toevoegen
public bool Toevoegen(string table, string[] schema, params object[] fields )
{
MessageBox.Show("DatabaseManager::Toevoegen() invoked", "DatabaseManager");
//m_cnADONewConnection.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\meetSysteem.mdb";
OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(dataAdapter);
Gebruiker nieuweGebruiker = new Gebruiker();
GebruikerView nieuweGebruikerView = new GebruikerView();
//Begin opslaan
DataRow drNewRow = m_dtgebruiker.NewRow();
DataTable dtgebruiker = new DataTable();
DataTable dtDataTable; //new DataTable();
//dtDataTable = dsDataSet.Tables[0];
try
{
m_dtgebruiker.Rows.Add(drNewRow);
drNewRow["gebruikersnaam"] = "";
drNewRow["email"] = "hallo";
drNewRow["wachtwoord"] = "mooi";
drNewRow["wachtwoord"] = "mooi";
dataAdapter.Update(m_dtgebruiker);
m_rowPosition = m_dtgebruiker.Rows.Count - 1;
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
MessageBox.Show("DatabaseManager::Toevoegen() oeps", "DatabaseManager");
}
for (int i = 1; i < fields.Length; i++)
{
Console.Write(fields[i] + "");
}
Console.WriteLine();
//Einde opslaan
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);
}
//Methode verwijderen
public bool Verwijderen(string table, string[] keys, params object[] fields)
{
if (!(m_dtgebruiker.Rows.Count == 0))
{
m_dtgebruiker.Rows[m_rowPosition].Delete();
dataAdapter.Update(m_dtgebruiker);
m_rowPosition = 0;
//this.ShowCurrentRecord();
}
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);
}
//Methode 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;
}
}
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 abstract string Formatted(object o);
}
}
Maar hoe ik nu bijvoorbeeld de methode: Toevoegen gebruiken in een formulier met drie textvelden: 'gebruikersnaam' , ' email' en ' wachtwoord' . En dan is er een knop opslaan die ervoor zorgt dat het in de database komt te staan?
Alstublieft. Is er iemand die mij wat tips kan geven?