Esempio n. 1
0
        /// <summary>
        /// Returns ROWS from User Defined Custom Table. Requires TableInfo with information about columns and it's type of data.
        /// Obtainable with GetInfoAboutTables.
        /// </summary>
        /// <returns></returns>
        public static CustomTableRows GetAllRowsFromCustomTable(TableInfo customTableInfo)
        {
            List <CellContent> collection;
            CustomTableRows    customTableRow = new CustomTableRows();

            //TableInfo userDefinedTableContent = null;                                 TABLE CONTENTS ?
            string command = @"SELECT * FROM " + customTableInfo.TableName;

            var query = new MySqlCommand(command, dbAgent.GetConnection());

            dbAgent.GetConnection().Open();
            try
            {
                var reader = query.ExecuteReader();

                while (reader.Read())
                {
                    collection = new List <CellContent>();
                    // for every columns in row
                    for (int i = 0; i < customTableInfo.Count; i++)
                    {
                        // To determine what action we need to do, we need to check for every column type.
                        switch (customTableInfo.ColumnInfos_Row[i].ColumnType)
                        {
                        case ColumnType.DataType:
                            var DateTime = (DateTime)reader[customTableInfo.ColumnInfos_Row[i].Name];
                            collection.Add(new CellContent(DateTime, (int)reader["ID"]));
                            break;

                        case ColumnType.Description:
                            var descr = reader[customTableInfo.ColumnInfos_Row[i].Name].ToString();
                            collection.Add(new CellContent(descr, (int)reader["ID"]));
                            break;

                        case ColumnType.ShortText:
                            var shrtText = reader[customTableInfo.ColumnInfos_Row[i].Name].ToString();
                            collection.Add(new CellContent(shrtText, (int)reader["ID"]));
                            break;

                        case ColumnType.Numeric:
                            var num = (int)reader[customTableInfo.ColumnInfos_Row[i].Name];
                            collection.Add(new CellContent(num, (int)reader["ID"]));
                            break;

                        default: throw new Exception("Custom Table Helper GetAllRowsFromCustomTable method, switch Error ");
                        }
                    }
                    // ROWS
                    customTableRow.AddRow(collection);
                }
            }

            catch (MySqlException ex)
            {
                MessageBox.Show("Nastąpił błąd połączenia z bazą danych. Jeśli problem będzie się powtrzał skontaktuj się z zarządcą bazy danych", "Błąd połączenia z bazą danych" + Environment.NewLine + ex.ErrorCode, MessageBoxButtons.OK, MessageBoxIcon.Error);



                Debug.WriteLine("Exception Message: " + ex.Message);
                Debug.WriteLine("Exception Error Code: " + ex.ErrorCode);
                Debug.WriteLine("Exception Source: " + ex.Source);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Wystąpił  błąd " + Environment.NewLine + ex.Message + "Operacja nie powiodła się", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Debug.WriteLine("Exception Message: " + ex.Message);
                Debug.WriteLine("Exception Source: " + ex.Source);
                Debug.WriteLine(" Custom Table Helper GetAllRowsFromCustomDb");
            }
            finally
            {
                dbTools.dbAgent.GetConnection().Close();
            }

            return(customTableRow);
        }