Example #1
0
        public List <dynamic> ToPageList(int pageIndex, int pageSize, out int recordCount)
        {
            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.PagingSelect2008(_table, _fields, _where, _orderBy, pageIndex, pageSize);

            var command = new SqlCommand(sql);

            command.Parameters.AddRange(_parameters.Parameters);
            var param = new SqlParameter("@RecordCount", SqlDbType.Int)
            {
                Direction = ParameterDirection.Output
            };

            command.Parameters.Add(param);

            recordCount = 0;
            List <dynamic> result;

            using (var conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                command.Connection = conn;
                using (var sdr = command.ExecuteReader())
                {
                    var handler = new SqlDataReaderSelectConverter();
                    result = handler.ConvertToDynamicList(sdr);
                }
            }

            recordCount = (int)param.Value;
            return(result);
        }
        public static List <dynamic> Fetch(this SqlConnection conn, string sql, object parameters = null, SqlTransaction trans = null)
        {
            try
            {
                var command = new SqlCommand(sql, conn, trans);
                if (parameters != null)
                {
                    command.Parameters.AddRange(ConvertToDbParameter(parameters).Parameters);
                }

                var            mapper = new SqlDataReaderSelectConverter();
                List <dynamic> result;

                using (var sdr = command.ExecuteReader())
                {
                    result = mapper.ConvertToDynamicList(sdr);
                }

                return(result);
            }
            catch (Exception)
            {
                trans?.Rollback();
            }
            finally
            {
                conn.Close();
            }
            return(new List <dynamic>());
        }
Example #3
0
        public async Task <List <dynamic> > ToListAsync()
        {
            var sqlBuilder = new SqlServerBuilder();
            var sql        = sqlBuilder.Select(_table, _fields, _where, _orderBy);

            List <dynamic> result;
            var            visitor = new SqlDataReaderSelectConverter();

            using (var conn = new SqlConnection(_connectionString))
            {
                var command = new SqlCommand(sql, conn);
                command.Parameters.AddRange(_parameters.Parameters);
                conn.Open();
                using (var sdr = await command.ExecuteReaderAsync())
                {
                    result = visitor.ConvertToDynamicList(sdr);
                }
            }

            return(result);
        }