Esempio n. 1
0
        /// <summary>
        /// 执行SQL,并返回数据集
        /// </summary>
        /// <returns></returns>
        public DataSet GetDataSet()
        {
            var page = new DbPage();

            page.TotalRowCount = 0;
            page.PageIndex     = 0;
            page.PageRowCount  = 0;
            return(GetDataSet(page));
        }
Esempio n. 2
0
        /// <summary>
        /// 执行SQL,并返回数据集
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public DataTable GetDataTable(DbPage page)
        {
            var dataset = GetDataSet(page);

            if (dataset.Tables.Count > 0)
            {
                return(dataset.Tables[0]);
            }
            return(new DataTable());
        }
Esempio n. 3
0
        /// <summary>
        /// 执行SQL,并返回数据集
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public DataSet GetDataSet(DbPage page)
        {
            var reader = getDataReader();
            var result = new DataSet();

            try
            {
                var rowIdx = (page.PageIndex - 1) * page.PageRowCount;
                if (rowIdx < 0)
                {
                    rowIdx = 0;
                }

                var rowcount = page.PageRowCount;
                var total    = 0;
                var index    = 0;
                var count    = 0;
                do
                {
                    var dTable     = new DataTable(Guid.NewGuid().ToString("D"));
                    var fieldCount = reader.FieldCount;
                    if (fieldCount == 0)
                    {
                        break;
                    }
                    for (var i = 0; i < fieldCount; i++)
                    {
                        var dc = new DataColumn();
                        dc.ColumnName = GetDataColumnName(reader.GetName(i), dTable);
                        dc.DataType   = reader.GetFieldType(i);
                        dTable.Columns.Add(dc);
                    }

                    dTable.BeginLoadData();

                    while (reader.Read())
                    {
                        index++;
                        if (page.TotalRowCount == 0)
                        {
                            total++;
                        }
                        bool addRow = true;
                        if (rowIdx > 0 && rowcount > 0)
                        {
                            if (count >= rowcount)
                            {
                                break;
                            }
                            if (index <= rowIdx)
                            {
                                addRow = false;
                            }
                        }
                        if (addRow)
                        {
                            count++;
                            var row = dTable.NewRow();
                            for (var i = 0; i < fieldCount; i++)
                            {
                                if (reader.IsDBNull(i))
                                {
                                    row[i] = DBNull.Value;
                                }
                                else
                                {
                                    try
                                    {
                                        row[i] = reader.GetValue(i);
                                    }
                                    catch
                                    {
                                        row[i] = DBNull.Value;
                                    }
                                }
                            }
                            dTable.Rows.Add(row);
                        }
                    }
                    if (total > 0)
                    {
                        page.TotalRowCount = total;
                    }
                    dTable.EndLoadData();
                    result.Tables.Add(dTable);
                } while (reader.NextResult());
            }
            finally
            {
                doClose(reader);
            }
            return(result);
        }