/// <summary> /// Default constructor /// </summary> /// <param name="strTableName">Database table name</param> /// <param name="functions">Database functions object</param> public DataTableInformation(string strTableName, IDatabaseFunctions functions) { string strError = ""; // Validation if (TableName == "" || functions == null) { return; } // Instantiate Column List this.m_Columns = new DataColumnInformationList(); this.m_TableName = strTableName; // Get Table Exists this.m_TableExists = DatabaseInformation.CheckDataBaseTableExists(this.m_TableName, functions, ref strError); // Validation if (this.m_TableExists == false) { return; } // Load Table Information this.LoadTableInformationFromDatabase(strTableName, functions); }
/// <summary> /// Load data table information from a datatable /// </summary> /// <param name="dt">DataTable to load information from</param> /// <param name="strTableName">Database table name</param> private void LoadTableInformationFromDataTable(DataTable dt, string strTableName = "") { try { string strError = ""; // Validation if (dt == null || strError != "") { this.m_IsValid = false; return; } DataColumn[] cols = new DataColumn[dt.Columns.Count]; dt.Columns.CopyTo(cols, 0); //Create New List Of Columns List <DataColumn> listColumns = new List <DataColumn>(); listColumns.AddRange(cols); // Loop Columns foreach (DataColumn column in listColumns) { // Get Sql Type SqlDbType type = DatabaseInformation.GetSqlTypeFromType(column.DataType); // Get Max-length int intMaxLength = (column.MaxLength > 0) ? column.MaxLength : 255; // Create Column Info DataColumnInformation columnInfo = new DataColumnInformation(column.ColumnName, type, intMaxLength, column.AllowDBNull); this.m_Columns.Add(columnInfo); } this.m_IsValid = true; } catch (Exception ex) { // To Be Implemented: Throw Custom Exception... Console.WriteLine(ex.ToString()); this.m_IsValid = false; } }
/// <summary> /// Load table information from the database /// </summary> /// <param name="strTableName">Database table name to load information from</param> /// <param name="functions">IDatabaseFunctions object</param> private void LoadTableInformationFromDatabase(string strTableName, IDatabaseFunctions functions) { try { string strError = ""; // Get Query String string strQuery = SQL_GET_TABLE_INFO_QUERY + "'" + strTableName + "'"; // Get DataTable DataTable dt = functions.GetDataTable(strQuery, ref strError); // Validation if (dt == null || strError != "") { this.m_IsValid = false; return; } // Sanitize Rows Into String Lists List <List <string> > listRowLists = dt.AsEnumerable().Select(row => row.ItemArray.Select(v => (v == null || v == DBNull.Value) ? "" : v.ToString()).ToList()).ToList(); // Loop DataRows foreach (List <string> listRowValues in listRowLists) { // Validation if (listRowLists.Count < 4) { this.m_IsValid = false; return; } // Get Column Name string strColumnName = listRowValues[0]; // Get Sql Data Type SqlDbType dataType = DatabaseInformation.GetSqlTypeFromString(listRowValues[1]); // Get Field Length int intFieldLength = (int.TryParse(listRowValues[2], out intFieldLength) == true) ? int.Parse(listRowValues[2]) : 255; // Get Nullable bool boolIsNullable = (bool.TryParse(listRowValues[3], out boolIsNullable) == true) ? bool.Parse(listRowValues[3]) : true; // Create New Column DataColumnInformation column = new DataColumnInformation(strColumnName, dataType, intFieldLength, boolIsNullable); // Add Column To List this.m_Columns.Add(column); } this.m_IsValid = true; } catch (Exception ex) { // To Be Implemented: Throw Custom Exception... Console.WriteLine(ex.ToString()); this.m_IsValid = false; } }