Click here to Skip to main content
15,886,740 members
Articles / Database Development / SQL Server

Paging and Sorting on SQL Server and Oracle using Sql.Net

Rate me:
Please Sign up or sign in to vote.
4.79/5 (10 votes)
21 Feb 20055 min read 93.7K   742   36  
This article presents an alternative solution to paging large datasets using Open Source Sql.Net library.
using System;
using System.Data;
using System.Data.SqlClient;
using Reeb.SqlOM;
using Reeb.SqlOM.Render;

namespace PagingSampleProject
{
	class Test
	{
		SqlConnection connection;

		[STAThread]
		static void Main(string[] args)
		{
			Test test = new Test();
			test.AllProducts(1, 2, "name", true);
			test.ParametrizedQuery(0, 2, "name", true);
		}

		Test()
		{
			connection = new SqlConnection("your database here");
			connection.Open();
		}

		void AllProducts(int pageNum, int pageSize, string orderCol, bool asc)
		{
			SelectQuery query = new SelectQuery();
			query.Columns.Add(new SelectColumn("*"));
			query.FromClause.BaseTable = FromTerm.Table("products");
			query.OrderByTerms.Add(new OrderByTerm(orderCol, (asc) ? OrderByDirection.Ascending : OrderByDirection.Descending));

            SqlServerRenderer renderer = new SqlServerRenderer();
			string rowCountSql = renderer.RenderRowCount(query);
			int rowCount = Convert.ToInt32(new SqlCommand(rowCountSql, connection).ExecuteScalar());
			string pageSql = renderer.RenderPage(pageNum, pageSize, rowCount, query);
			IDataReader data = new SqlCommand(pageSql, connection).ExecuteReader();
			data.Close();
		}

		void Example_1_1(string orderCol, bool asc)
		{
			FromTerm tProducts = FromTerm.Table("products");
			FromTerm tCategories = FromTerm.Table("categories");

			SelectQuery query = new SelectQuery();
			query.Columns.Add(new SelectColumn("productName"));
			query.Columns.Add(new SelectColumn("categoryName"));
			query.FromClause.BaseTable = tProducts;
			query.FromClause.Join(JoinType.Inner, tProducts, tCategories, "categoryId", "categoryId");
			query.OrderByTerms.Add(new OrderByTerm(orderCol, (asc) ? OrderByDirection.Ascending : OrderByDirection.Descending));	
		}

		void ParametrizedQuery(int pageNum, int pageSize, string orderCol, bool asc)
		{
			int categoryId = 1;

			//Create the query
			SelectQuery query = new SelectQuery();
			query.Columns.Add(new SelectColumn("*"));
			query.FromClause.BaseTable = FromTerm.Table("products");
			query.OrderByTerms.Add(new OrderByTerm(orderCol, (asc) ? OrderByDirection.Ascending : OrderByDirection.Descending));

			//Set filter
			query.WherePhrase.Terms.Add(WhereTerm.CreateCompare(SqlExpression.Number(categoryId), SqlExpression.Field("productId"), CompareOperator.Equal));

			//Prepare renderer and SqlCommand
			SqlServerRenderer renderer = new SqlServerRenderer();
			SqlCommand command = new SqlCommand();
			command.Connection = connection;
			command.Parameters.Add("@param1", categoryId);

			//Render & Execute
			command.CommandText = renderer.RenderRowCount(query);
			int rowCount = (int)command.ExecuteScalar();
			command.CommandText = renderer.RenderPage(pageNum, pageSize, rowCount, query);
			IDataReader data = command.ExecuteReader();
			
			data.Close();
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions