Esempio n. 1
0
        public async Task <T> SelectAsync <T>(object primaryKey)
            where T : class, new()
        {
            var tableName           = typeof(T).TableNameValidate();
            var primaryKeyAttribute = AttributeExtension.PrimaryKeyValidate(typeof(T).GetProperties());
            var query  = $"SELECT * FROM {tableName} WHERE {primaryKeyAttribute.Name} = @{primaryKeyAttribute.Name}";
            var result = (await ExecuteReaderAsync <T>(query, new[] {
                new TParameter()
                {
                    ParameterName = primaryKeyAttribute.Name,
                    Value = primaryKey
                }
            })).FirstOrDefault();

            return(result);
        }
Esempio n. 2
0
        public static T Select <T, TDatabaseType, TParameter>(string connectionString, object primaryKey)
            where T : class, new()
            where TDatabaseType : DbConnection, new()
            where TParameter : DbParameter, new()
        {
            var tableName           = typeof(T).TableNameValidate();
            var primaryKeyAttribute = AttributeExtension.PrimaryKeyValidate(typeof(T).GetProperties());
            var query  = $"SELECT * FROM {tableName} WHERE {primaryKeyAttribute.Name} = @{primaryKeyAttribute.Name}";
            var result = ExecuteReader <T, TDatabaseType>(connectionString, query, new[] {
                new TParameter()
                {
                    ParameterName = primaryKeyAttribute.Name,
                    Value         = primaryKey
                }
            }).FirstOrDefault();

            return(result);
        }