Beispiel #1
0
        /// <summary>
        /// Saves the <seealso cref="DBObject"/> to the datasource.
        /// </summary>
        /// <param name="dbObject"><seealso cref="DBObject"/> to save to the datasource.</param>
        /// <param name="saveType">Specifies how to save the <seealso cref="DBObject"/>.</param>
        ///
        /// <example>
        /// The following example shows how to create and saves an <seealso cref="Admin"/> to the datasource.
        /// <code>
        /// Admin admin = new Admin();
        /// DBController.Save(admin, DBObject.SaveTypes.Insert);
        /// </code>
        /// </example>
        ///
        /// <example>
        /// The following example shows how to update and save an <seealso cref="Admin"/> currently in the datasource.
        /// <code>
        /// foreach (Admin admin in DBController.GetAllRecords&lt;Admin&gt;())
        /// {
        ///    if (admin.Username == "user123") // or other unique identifier
        ///    {
        ///       admin.FirstName = "myNewFirstName";
        ///       DBController.Save(admin, DBObject.SaveTypes.Update);
        ///    }
        /// }
        /// </code>
        /// </example>
        ///
        /// <returns>
        /// Whether the save was successful or not.
        /// </returns>
        public static bool Save(DBObject dbObject, SaveTypes saveType)
        {
            string query;

            switch (saveType)
            {
            case SaveTypes.Insert:
                dbObject.SetPrimaryKey(dbObject.NextAvailablePrimaryKey());
                query = CreateInsertString(dbObject);
                break;

            case SaveTypes.Update:
                query = CreateUpdateString(dbObject);
                break;

            default:
                throw new NotImplementedException($"SaveType {saveType.ToString()} has not been implemented in the Save method.");
            }
            using (OleDbConnection dbConnection = new OleDbConnection(CONNECTION_STRING))
            {
                using (OleDbCommand command = new OleDbCommand(query, dbConnection))
                {
                    AddValuesToPropertyPlaceholders(command, saveType, dbObject);
                    dbConnection.Open();
                    return(command.ExecuteNonQuery() == 1);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Searches the datasource for records of the specified <paramref name="type"/>, creates a
        /// <seealso cref="DBObject"/> for each record, and returns a list of those created <seealso cref="DBObject"/>s.
        /// </summary>
        /// <param name="type">Type of <seealso cref="DBObject"/>.</param>
        ///
        /// <returns>
        /// List of created <seealso cref="DBObject"/>s.
        /// </returns>
        internal static List <DBObject> GetAllRecords(Type type)
        {
            List <DBObject> list = new List <DBObject>();

            using (OleDbConnection dbConnection = new OleDbConnection(CONNECTION_STRING))
            {
                string table = type.Name;
                string query = $"SELECT * FROM {table}";
                try
                {
                    using (OleDbCommand command = new OleDbCommand(query, dbConnection))
                    {
                        dbConnection.Open();
                        using (OleDbDataReader dbDataReader = command.ExecuteReader())
                        {
                            if (dbDataReader.HasRows)
                            {
                                foreach (DbDataRecord record in dbDataReader)
                                {
                                    DBObject dbObject = Activator.CreateInstance(type) as DBObject;
                                    SetPropertyValuesFromRecord(ref dbObject, record);
                                    dbObject.SetPrimaryKey(record);
                                    list.Add(dbObject);
                                }
                            }
                        }
                    }
                }
                catch (OleDbException)
                {
                    throw;
                }
            }
            return(list);
        }