using System; using System.IO; using System.Data; using System.Data.SqlClient; using Npgsql; using System.Xml; using Mono; namespace WoWR.Database.Connection { /// /// Class for Handling the Connection to the ADO.Net Layer of the Databases. /// Funktions for loading and storing the complete Dataset are in there. /// public class DataConnection { string connString; ConnectionType connType; /// /// Constructor to set up a Database /// /// Connection-Type the Database should use /// Connection-String to indicate the Parameters of the Datasource. /// XML = Directory where the XML-Files sould be stored /// MYSQL = ADO.NET ConnectionString /// MSSQL = ADO.NET ConnectionString /// OLEDB = ADO.NET ConnectionString /// ODBC = ADO.NET ConnectionString /// public DataConnection(ConnectionType connType, string connString) { this.connType = connType; this.connString = connString; //if Directory has no trailing \ than append it ;-) if(connType == ConnectionType.DATABASE_XML) { if(connString[connString.Length - 1] != '\\') this.connString = connString + "\\"; if(!Directory.Exists(connString)) { try { Directory.CreateDirectory(connString); } catch(Exception) { } } } } /// /// Load an Dataset with the a Table /// /// Name of the Table to Load in the DataSet /// DataSet that sould be filled /// public void LoadDataSet(string tableName, DataSet dataSet) { dataSet.Clear(); switch(connType) { case ConnectionType.DATABASE_XML: { string filename = connString + tableName + ".xml"; try { dataSet.ReadXml(filename); dataSet.AcceptChanges(); } catch ( System.IO.FileNotFoundException ) { try { dataSet.WriteXml(filename); dataSet.WriteXmlSchema(connString + tableName + ".xsd"); } catch (Exception ex) { throw new DatabaseException("Could not create XML-Databasefiles (Directory present ?)", ex); } } break; } case ConnectionType.DATABASE_PGSQL: { try { NpgsqlConnection conn = new NpgsqlConnection(connString); NpgsqlDataAdapter adapter = new NpgsqlDataAdapter("SELECT * from " + tableName , conn); adapter.Fill(dataSet.Tables[tableName]); } catch (Exception ex) { throw new DatabaseException("Could not load the Database-Table", ex); } break; } } } /// /// Writes all Changes in a Dataset to the Table /// /// Name of the Table to update /// DataSet set contains the Changes that sould be written /// public void SaveDataSet(string tableName, DataSet dataSet) { if(dataSet.HasChanges() == false) return; switch(connType) { case ConnectionType.DATABASE_XML: { try { dataSet.WriteXml(connString + tableName + ".xml"); dataSet.AcceptChanges(); dataSet.WriteXmlSchema(connString + tableName + ".xsd"); } catch (Exception e) { throw new DatabaseException("Could not save Databases in XML-Files!", e); } break; } case ConnectionType.DATABASE_PGSQL: { try { NpgsqlConnection conn = new NpgsqlConnection(connString); NpgsqlDataAdapter adapter = new NpgsqlDataAdapter("SELECT * from " + tableName, conn); NpgsqlCommandBuilder builder = new NpgsqlCommandBuilder(adapter); conn.Open(); adapter.DeleteCommand = builder.GetDeleteCommand(); //the errors is in those lines though adapter.UpdateCommand = builder.GetUpdateCommand(); adapter.InsertCommand = builder.GetInsertCommand(); DataSet changes = dataSet.GetChanges(); adapter.Update(changes, tableName); dataSet.AcceptChanges(); } catch (Exception ex) { throw new DatabaseException("Could not save the Database-Table", ex); } break; } } } } }