我喜欢参考我从这个网址获取的代码
https://msdn.microsoft.com/en-us/library/ms171624%28v=vs.110%29.aspx
上面的 url 显示了我们如何通过datagridview
按需加载数据意味着当用户滚动并达到限制时,数据将从 db 加载。我的代码正在运行,但现在我必须使用来自 SQL Server 的存储过程,该过程非常大,并且经过多次计算后返回数据。所以我必须自定义我的代码,因此它应该使用存储过程而不是内联 SQL。从上面的链接中,任何人都可以看到完整的工作示例代码。我只是在这里突出显示 3 个例程,从其中触发内联 SQL 以从 db 获取数据,我需要更改这个例程并希望从那里调用存储过程而不是内联 SQL。
从这一路线使用内联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;
}
现在我想在一个例程中使用一个存储过程来替换其他两个例程。
我将开发存储过程,第一个结果将返回行数,第二个结果将返回实际数据。
这样我在存储过程中分页:
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)
请帮助我提供代码和示例。谢谢