Exemplo n.º 1
0
        /// <summary>
        /// Get the specified instance of the type.
        /// </summary>
        /// <param name="id">Instance primary key.</param>
        /// <returns>The specified instance.</returns>
        private static T ReadFromDatabase(long id, bool closeConnection = true)
        {
            T             instance = default;
            ORMSqlCommand cmd      = ORMEntity <T> .SqlDialect.GetSelectCommand();

            // Connecto to database
            ORMEntity <T> .Connect();

            // Set command parameters
            ORMEntity <T> .SetParameter(cmd.PrimaryKeyName, id);

            // Execute the SELECT sentence to retrieve the instance properties
            using (DbDataReader reader = ORMEntity <T> .ExecuteReader(cmd.SqlCommand))
            {
                if (reader.Read())
                {
                    instance = ORMEntity <T> .MapData(reader);
                }
            }

            // Close the connection to database
            if (closeConnection)
            {
                ORMEntity <T> .Disconnect();
            }

            return(instance);
        }