我想引用我从此网址
获取的代码https://msdn.microsoft.com/en-us/library/ms171624%28v=vs.110%29.aspx
上面的网址显示了我们如何通过datagridview
按需加载数据意味着当用户滚动并达到限制时数据将从db加载。我的代码正在运行但现在我处于这样的情况:我必须使用SQL Server中的存储过程非常大并且在经过如此多的计算后返回数据。所以我必须自定义我的代码,因此它应该使用存储过程而不是内联SQL。从上面的链接,任何人都可以看到完整的工作示例代码我只是在这里突出显示3个例程,其中内联SQL触发从db获取数据,这个例程我需要更改并希望从那里调用存储过程而不是内联SQL。
从这一个路由使用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;
}
}
此例程也使用内联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;
}
}
这个例程也是
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;
}
现在我想在一个例程中使用存储过程来替换其他两个例程。
我将开发存储过程,第一个结果将返回no行,第二个结果将返回实际数据。
这样我在存储过程中进行分页:
WHERE [rn] BETWEEN ((@StartIndex-1) * @EndIndex ) + 1 AND (@StartIndex * @EndIndex)
现在看一下名为SupplyPageOfData
的上述例程如何进行分页并告诉我如何使用lowerPageBoundary
和rowsPerPage
因此我可以在sp和sp中发送这两个值可以成功分页。
rowsPerPage
已修复为16但lowerPageBoundary
跳过16.因此我不会想到如何更改代码的计算结果我可以发送lowerPageBoundary
& rowsPerPage
到存储过程,在存储过程中,我可以形成像WHERE [rn] BETWEEN ((@StartIndex-1) * @EndIndex ) + 1 AND (@StartIndex * @EndIndex)
请帮我处理代码和示例。感谢