Sylloge
A C# helper library
code/Data/Database.cs
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 // TODO: refactor
00018 namespace Sylloge.Data
00019 {
00023     public enum DatabaseType
00024     {
00028         ODBC,
00032         OLEDB
00033     }
00034 
00038     public abstract class Database : System.IDisposable
00039     {
00040         #region Class Members
00041 
00042         #region Protected Members
00043 
00044         protected System.Data.Common.DbDataAdapter m_Adapter { get; set; }
00045         protected System.Data.Common.DbConnection m_Conn { get; set; }
00046         protected string m_ConnStr { get; set; }
00047         protected bool m_IsOpen { get; set; }
00048         protected string m_Name { get; set; }
00049         protected System.Data.DataTable m_Table { get; set; }
00050         protected Sylloge.Data.DatabaseType m_Type { get; set; }
00051 
00052         #endregion
00053 
00054         #region Public Members
00055 
00059         public System.Data.DataSet Dataset { get; protected set; }
00060 
00064         public Sylloge.Data.DatabaseType Type
00065         {
00066             get
00067             {
00068                 return this.m_Type;
00069             }
00070         }
00071 
00072         #endregion
00073 
00074         #endregion
00075 
00076         #region Class Methods
00077 
00081         public Database(Sylloge.Data.DatabaseType type)
00082         {
00083             this.Initialize(type);
00084         }
00085 
00089         ~Database()
00090         {
00091             this.Dispose();
00092         }
00093 
00097         public void Dispose()
00098         {
00099             this.Close();
00100             System.GC.SuppressFinalize(this);
00101         }
00102 
00106         public void Close()
00107         {
00108             if (this.m_Conn != null) {
00109                 this.m_Conn.Close();
00110                 this.m_Conn.Dispose();
00111             }
00112             if (this.m_Adapter != null) { this.m_Adapter.Dispose(); }
00113             if (this.Dataset != null) { this.Dataset.Dispose(); }
00114             if (this.m_Table != null) { this.m_Table.Dispose(); }
00115             this.Dataset = null;
00116             this.m_Adapter = null;
00117             this.m_Conn = null;
00118             this.m_Table = null;
00119             this.m_IsOpen = false;
00120         }
00121 
00127         public bool ColumnExists(string columnName)
00128         {
00129             return this.m_Table.Columns.Contains(columnName);
00130         }
00131 
00136         public System.Data.DataTable GetTables()
00137         {
00138             System.Data.DataTable dt = null;
00139             if (this.m_Type == Sylloge.Data.DatabaseType.ODBC) {
00140                 dt = ((System.Data.Odbc.OdbcConnection)this.m_Conn).GetSchema("Tables");
00141             } else {
00142                 dt = ((System.Data.OleDb.OleDbConnection)this.m_Conn).GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
00143             }
00144             return dt;
00145         }
00146 
00150         private void Initialize(Sylloge.Data.DatabaseType type)
00151         {
00152             this.m_Type = type;
00153             if (this.m_Type == Sylloge.Data.DatabaseType.ODBC) {
00154                 this.m_Conn = new System.Data.Odbc.OdbcConnection();
00155                 this.m_Adapter = new System.Data.Odbc.OdbcDataAdapter();
00156                 this.m_Adapter.DeleteCommand = new System.Data.Odbc.OdbcCommand();
00157                 this.m_Adapter.InsertCommand = new System.Data.Odbc.OdbcCommand();
00158                 this.m_Adapter.SelectCommand = new System.Data.Odbc.OdbcCommand();
00159                 this.m_Adapter.UpdateCommand = new System.Data.Odbc.OdbcCommand();
00160             } else {
00161                 this.m_Conn = new System.Data.OleDb.OleDbConnection();
00162                 this.m_Adapter = new System.Data.OleDb.OleDbDataAdapter();
00163                 this.m_Adapter.DeleteCommand = new System.Data.OleDb.OleDbCommand();
00164                 this.m_Adapter.InsertCommand = new System.Data.OleDb.OleDbCommand();
00165                 this.m_Adapter.SelectCommand = new System.Data.OleDb.OleDbCommand();
00166                 this.m_Adapter.UpdateCommand = new System.Data.OleDb.OleDbCommand();
00167             }
00168             this.m_Adapter.AcceptChangesDuringUpdate = true;
00169             this.m_Adapter.AcceptChangesDuringFill = true;
00170             this.Dataset = new System.Data.DataSet();
00171             this.m_Table = new System.Data.DataTable();
00172         }
00173 
00177         public bool IsConnected
00178         {
00179             get
00180             {
00181                 return (this.m_Conn != null) && this.m_IsOpen;
00182             }
00183         }
00184 
00189         public bool Open()
00190         {
00191             return this.Open(this.m_Name, this.m_ConnStr);
00192         }
00193 
00200         private bool Open(string dbName, string connString)
00201         {
00202             if (dbName == null || dbName.Trim() == string.Empty ||
00203                 connString == null || connString.Trim() == string.Empty) {
00204                 return false;
00205             }
00206             this.Close();
00207             this.Initialize(this.m_Type);
00208             this.m_Name = dbName;
00209             this.m_ConnStr = connString;
00210             this.m_IsOpen = false;
00211             try {
00212                 this.m_Conn.ConnectionString = connString;
00213                 this.m_Conn.Open();
00214                 this.m_Adapter.DeleteCommand.Connection = this.m_Conn;
00215                 this.m_Adapter.InsertCommand.Connection = this.m_Conn;
00216                 this.m_Adapter.SelectCommand.Connection = this.m_Conn;
00217                 this.m_Adapter.UpdateCommand.Connection = this.m_Conn;
00218                 this.m_IsOpen = true;
00219             } catch (System.Exception ex) {
00220                 // Reason for this could be invalid password
00221                 Sylloge.App.Out("There was an error while trying to open the database.", ex);
00222                 this.m_IsOpen = false;
00223             }
00224             return this.m_IsOpen;
00225         }
00226 
00234         public System.Data.DataTable Query(string table, string query)
00235         {
00236             if (!this.Dataset.Tables.Contains(table)) {
00237                 throw new System.Exception("Invalid table specified");
00238             }
00239             try {
00240                 this.Dataset.Clear();
00241                 // We specifically cast here to ensure the queries are sent/received properly
00242                 if (this.m_Type == Sylloge.Data.DatabaseType.ODBC) {
00243                     ((System.Data.Odbc.OdbcDataAdapter)this.m_Adapter).SelectCommand.CommandText = query;
00244                     ((System.Data.Odbc.OdbcDataAdapter)this.m_Adapter).SelectCommand.ExecuteNonQuery();
00245                     ((System.Data.Odbc.OdbcDataAdapter)this.m_Adapter).Fill(this.Dataset, table);
00246                 } else {
00247                     ((System.Data.OleDb.OleDbDataAdapter)this.m_Adapter).SelectCommand.CommandText = query;
00248                     ((System.Data.OleDb.OleDbDataAdapter)this.m_Adapter).SelectCommand.ExecuteNonQuery();
00249                     ((System.Data.OleDb.OleDbDataAdapter)this.m_Adapter).Fill(this.Dataset, table);
00250                 }
00251                 return this.Dataset.Tables[table];
00252             } catch (System.Data.Odbc.OdbcException odbcex) {
00253                 if (this.m_Type != Sylloge.Data.DatabaseType.ODBC) {
00254                     throw new System.Exception(("Unexpected ODBC error for table '" + table + "': " + odbcex.Message), odbcex);
00255                 }
00256                 throw new System.Exception(("ODBC error querying table '" + table + "': " + odbcex.Message), odbcex);
00257             } catch (System.Data.OleDb.OleDbException oledbex) {
00258                 if (this.m_Type != Sylloge.Data.DatabaseType.OLEDB) {
00259                     throw new System.Exception(("Unexpected OLEDB error for table '" + table + "': " + oledbex.Message), oledbex);
00260                 }
00261                 throw new System.Exception(("Error querying table : '" + table + "'" + oledbex.Message), oledbex);
00262             } catch (System.Exception ex) {
00263                 throw new System.Exception(("Error querying table '" + table + "': " + ex.Message), ex);
00264             }
00265         }
00266 
00267         #endregion
00268     }
00269 
00273     public class OleDb : Sylloge.Data.Database
00274     {
00278         public OleDb() : base(Sylloge.Data.DatabaseType.OLEDB) { }
00279     }
00280 
00284     public class Odbc : Sylloge.Data.Database
00285     {
00289         public Odbc() : base(Sylloge.Data.DatabaseType.ODBC) { }
00290     }
00291 }
 All Classes Namespaces Files Functions Variables Enumerations Properties Events