mercredi 23 septembre 2015

How to compatible datagridview on demand data loading with store procedure instead of in-line sql

i like to refer a code which i have taken from this url

http://ift.tt/1NMgULN

the above url shown how we can load data by datagridview just on demand means when user scroll and reach a limit then data will load from db. my code is working but now i am in situation that i have to use store procedure from sql server which is very big and it return data after so many calculation. so i have to customize my code as a result it should be working with store procedure instead of in-line sql. from the above link anyone can see full working sample code. i just here highlight 3 routine from where in-line sql fired to fetch data from db and this routine i need to change and want to call store proc from there instead of in-line sql.

from this one route use in-line sql

public int RowCount
        {
            get
            {
                // Return the existing value if it has already been determined. 
                if (rowCountValue != -1)
                {
                    return rowCountValue;
                }

                if (filters.Trim().ToUpper().IndexOf("WHERE") > -1)
                {
                    filters = filters.ToUpper().Replace("WHERE", string.Empty);
                }

                // Retrieve the row count from the database.
                command.CommandText = "SELECT COUNT(*) FROM " + tableName + " WHERE 1=1 " + (filters.Trim().Length > 0 ? " AND " : string.Empty) + filters;
                rowCountValue = (int)command.ExecuteScalar();
                return rowCountValue;
            }
        }

this routine too use in-line sql

public DataColumnCollection Columns
        {
            get
            {
                // Return the existing value if it has already been determined. 
                if (columnsValue != null)
                {
                    return columnsValue;
                }

                // Retrieve the column information from the database.
                command.CommandText = "SELECT * FROM " + tableName;
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = command;
                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                adapter.FillSchema(table, SchemaType.Source);
                columnsValue = table.Columns;
                return columnsValue;
            }
        }

this routine too

public DataTable SupplyPageOfData(int lowerPageBoundary, int rowsPerPage)
        {
            // Store the name of the ID column. This column must contain unique  
            // values so the SQL below will work properly. 
            if (columnToSortBy == null)
            {
                columnToSortBy = this.Columns[0].ColumnName;
            }

            if (!this.Columns[columnToSortBy].Unique)
            {
                throw new InvalidOperationException(String.Format(
                    "Column {0} must contain unique values.", columnToSortBy));
            }

            // Retrieve the specified number of rows from the database, starting 
            // with the row specified by the lowerPageBoundary parameter.
            if (filters.Trim().ToUpper().IndexOf("WHERE") > -1)
            {
                filters = filters.ToUpper().Replace("WHERE", string.Empty);
            }

            command.CommandText = "Select Top " + rowsPerPage + " " +
                CommaSeparatedListOfColumnNames + " From " + tableName +
                " WHERE 1=1 AND " + filters + " " + (filters.Trim().Length > 0 ? " AND " : string.Empty) + columnToSortBy + " NOT IN (SELECT TOP " +
                lowerPageBoundary + " " + columnToSortBy + " From " +
                tableName + "  WHERE 1=1 " + (filters.Trim().Length > 0 ? " AND " : string.Empty) + filters + " Order By " + sortColumn +
                ") Order By " + sortColumn;
            adapter.SelectCommand = command;

            DataTable table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            adapter.Fill(table);
            return table;
        }

now i want to use store proc in one routine which replace other two routine.

i will develop store proc whose 1st result will return no of rows and second result will return actual data.

this way i am paging in sp

WHERE [rn] BETWEEN ((@StartIndex-1) * @EndIndex ) + 1 AND (@StartIndex * @EndIndex)

now see the above routine called SupplyPageOfData how it is doing paging and tell me how to use lowerPageBoundary and rowsPerPage as a result i could send those two value in my sp and sp can do the successfully paging.

rowsPerPage is fixed that is 16 but lowerPageBoundary jump by 16. so calculation is not coming to my mind like how to change code as a result i can send lowerPageBoundary & rowsPerPage to store proc and with in store proc i can form paging line like WHERE [rn] BETWEEN ((@StartIndex-1) * @EndIndex ) + 1 AND (@StartIndex * @EndIndex)

please help me with code and sample. thanks

Aucun commentaire:

Enregistrer un commentaire