using System;
using System.Data;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
using System.Threading;
using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace WmData{
///
/// This is a DataAdapter wrapper, to be used with our object based data system.
/// Created 12th of May 2006.
///
/// Updated 22th of October 2007 with late thread based updates and enhanced command
/// creation.
///
public class WmDataAdapter {
protected WmData data;
protected WmDataBaseCredentials dataBaseCredentials;
protected string tableName;
protected DataTable table;
protected Type adaptedObjectType;
protected MySqlConnection connection;
protected MySqlDataAdapter adapter;
protected string[] columnNames;
protected MySqlDbType[] columnDbTypes;
public WmDataAdapter(WmData data, WmDataBaseCredentials dataBaseCredentials, Type adaptedObjectType){
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
this.data = data;
this.dataBaseCredentials = dataBaseCredentials;
this.adaptedObjectType = adaptedObjectType;
tableName = WmDataTools.getTableName(adaptedObjectType);
// Check if table exists in database backend
if(!WmDataBaseCreator.isTableExistent(tableName,dataBaseCredentials)){
// If not, make a glorious attempt to create the table
WmDataBaseCreator.createTable(adaptedObjectType,dataBaseCredentials);
}
createColumnArrays(adaptedObjectType);
#region Select Command
string selectSql = "SELECT #PROPERTIES# FROM "+dataBaseCredentials.Catalog+"."+tableName;
string propTmp = "";
for(int i = 0; i
/// Use this method to fill or refresh the datatable in the dataset with live data from database
///
public void fill(){
lock(table){
try{
connection.Open();
adapter.Fill(data.WmDataSet,tableName);
}catch(Exception error){
Trace.WriteLine("WmDataAdapter: Error while attempting to fill datatable "+tableName+": "+error.Message);
}finally{
connection.Close();
}
}
}
public void getSchema(){
try{
connection.Open();
adapter.FillSchema(data.WmDataSet, SchemaType.Source, tableName);
}catch(Exception error){
throw new Exception("WmDataAdapter: SQL Table mismatch with .NET object model. Datatable "+tableName+": "+error.Message);
}finally{
connection.Close();
}
}
///
/// Use this method to update, insert or delete any objects from/into database backend.
///
public void update(){
lock (table) {
try {
connection.Open();
// DateTime start = DateTime.Now;
// Trace.WriteLine("WmDataAdapter: Updating backend for table " + tableName);
int updated = adapter.Update(data.WmDataSet, tableName);
// Trace.WriteLine("WmDataAdapter: Done update for " + tableName + " in " + (DateTime.Now - start));
// table.AcceptChanges();
//Trace.WriteLine("WmDataAdapter: " + updated + " rows updated by calling update for table " + tableName + ". ");
} catch (Exception error) {
Trace.WriteLine("WmDataAdapter: Error while attempting to update datatable " + tableName + ": " + error.Message);
} finally {
connection.Close();
}
}
}
///
/// Gets the table name (object name) where this adapter is working for
///
public string TableName{
get { return tableName; }
}
///
/// Writes error information into trace when update / delete / insert fails
///
///
///
private void adapter_RowUpdated(object sender, MySqlRowUpdatedEventArgs e) {
if(e.RecordsAffected == 0){
Trace.WriteLine("WmDataAdapter: Excecuted Command, but no records affected: "+e.Command.CommandText);
foreach(MySqlParameter param in e.Command.Parameters){
Trace.WriteLine(param.ParameterName+" / "+param.DbType+" / "+param.Size +" / "+param.SourceColumn+" / "+param.Value);
}
}
}
}
}