SqlDatasource:Select() 上的参数

SqlDatasource: Parameters on Select()

看完https://msdn.microsoft.com/es-es/library/system.web.ui.webcontrols.sqldatasource.select(v=vs.110).aspx

我对 SqlDatasource 的 Select() 方法有点困惑。用一个例子很容易解释。让我们考虑这个测试代码:

//sql is a SqlDatasource, let's get out the question the initialization.
sql.SelectCommand= "select * from employees where iddepartment = @iddeparment";
sql.SelectParameters.Add("iddepartment ", DbType.Int, "1");

此时,我需要检索数据,所以我尝试做类似的事情:

IEnumerable<object> myData = sql.Select();

但是根据 MSDN 文档,Select 方法需要 1 个 DataSourceSelectArguments 类型的参数。这让我很困惑,如果我已经通过调用 SelectParameter.Add(...) 设置了一些 "Select parameters",为什么还要将它们作为参数传递?如何正确调用此函数?

DataSourceSelectArguments 不是你想的那样。对于您的情况,您可以简单地传递 DataSourceSelectArguments.Empty

Data-bound controls use the DataSourceSelectArguments class to request that a data source control performs additional data-related operations on a result set, such as sorting the data or returning a specific subset of data. These data-related operations are enumerated by the DataSourceCapabilities enumeration. The following table indicates how the DataSourceSelectArguments class supports these data-related operations.

此参数可以包含有关要应用的过滤器或要排序的列的信息。例如,在使用可排序的 GridView 时,对列进行排序会调用 Select() 方法,并传入一个 DataSourceSelectArguments 实例,并将其 SortExpression 属性 设置为列名用户选择排序依据。如果你不想让DataSource排序或者过滤,你传入DataSourceSelectArguments.Empty.