예제 #1
0
        protected virtual T GetObjectByID <T>(int Id, string procedureName, CreateObjectFromReaderMethod <T> method)
        {
            try
            {
                using (MySqlConnection con = this.OpenConnection())
                {
                    MySqlCommand command = new MySqlCommand(procedureName, con);
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    command.Parameters.Add(new MySqlParameter("pID", MySqlDbType.Int32)).Value = Id;
                    MySqlDataReader dr = command.ExecuteReader(CommandBehavior.SingleRow);
                    if (!dr.HasRows)
                    {
                        return(default(T));
                    }
                    dr.Read();
                    return(method(dr));
                }
            }
            catch (System.Exception ex)
            {
                Logging.Log.Create("Greška prilikom učitavanja objekta " + typeof(T).Name,
                                   Logging.LogEntryLevel.Critical, ex);
            }

            return(default(T));
        }
예제 #2
0
        /// <summary>
        /// Load objects from database using MySqlDataReader.
        /// </summary>
        /// <typeparam name="T">Object type.</typeparam>
        /// <param name="useReflection">If true we are going to use reflection while creating objects.</param>
        /// <param name="procedureName">Stored Procedure name.</param>
        /// <param name="method">Method which should create object of type T from data reader.</param>
        /// <returns>List of objects.</returns>
        protected virtual List <T> GetObjectAll <T>(string procedureName, CreateObjectFromReaderMethod <T> method)
        {
            List <T> objList = new List <T>();

            try
            {
                using (MySqlConnection con = this.OpenConnection())
                {
                    MySqlCommand command = new MySqlCommand(procedureName, con);
                    command.CommandType = System.Data.CommandType.StoredProcedure;
                    MySqlDataReader dr = command.ExecuteReader();

                    while (dr.Read())
                    {
                        T obj = method(dr);
                        if (obj != null)
                        {
                            objList.Add(obj);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Logging.Log.Create("Greška prilikom učitavanja objekta " + typeof(T).Name,
                                   Logging.LogEntryLevel.Critical, ex);
            }

            return(objList);
        }