DataSourceView.ExecuteSelect(DataSourceSelectArguments) 方法

定义

从基础数据存储获取数据列表。

C#
protected internal abstract System.Collections.IEnumerable ExecuteSelect(System.Web.UI.DataSourceSelectArguments arguments);

参数

arguments
DataSourceSelectArguments

用于请求对数据执行基本数据检索以外的操作的 DataSourceSelectArguments

返回

来自基础数据存储的数据的 IEnumerable 列表。

示例

下面的代码示例演示如何在扩展 DataSourceViewExecuteSelect的类中重写 方法。 打开 CsvDataSourceView 逗号分隔值 (.csv) 文件,逐行分析它,并创建 对象 DataTableDataView 对象以将数据保存在内存中。 最后,如果对象提供 DataSourceSelectArguments 排序表达式,并且 DataView 对象作为 IEnumerable 实例返回,则应用排序表达式。 此代码示例是为 DataSourceView 类提供的一个更大示例的一部分。

C#
// Get data from the underlying data source.
// Build and return a DataView, regardless of mode.
protected override IEnumerable ExecuteSelect(DataSourceSelectArguments selectArgs) {
    IEnumerable dataList = null;
    // Open the .csv file.
    if (File.Exists(this.SourceFile)) {
        DataTable data = new DataTable();

        // Open the file to read from.
        using (StreamReader sr = File.OpenText(this.SourceFile)) {
            // Parse the line
            string s = "";
            string[] dataValues;
            DataColumn col;

            // Do the following to add schema.
            dataValues = sr.ReadLine().Split(',');
            // For each token in the comma-delimited string, add a column
            // to the DataTable schema.
            foreach (string token in dataValues) {
                col = new DataColumn(token,typeof(string));
                data.Columns.Add(col);
            }

            // Do not add the first row as data if the CSV file includes column names.
            if (!IncludesColumnNames)
                data.Rows.Add(CopyRowData(dataValues, data.NewRow()));

            // Do the following to add data.
            while ((s = sr.ReadLine()) != null) {
                dataValues = s.Split(',');
                data.Rows.Add(CopyRowData(dataValues, data.NewRow()));
            }
        }
        data.AcceptChanges();
        DataView dataView = new DataView(data);
        if (!string.IsNullOrEmpty(selectArgs.SortExpression)) {
            dataView.Sort = selectArgs.SortExpression;
        }
        dataList = dataView;
    }
    else {
        throw new System.Configuration.ConfigurationErrorsException("File not found, " + this.SourceFile);
    }

    if (null == dataList) {
        throw new InvalidOperationException("No data loaded from data source.");
    }

    return dataList;
}

private DataRow CopyRowData(string[] source, DataRow target) {
    try {
        for (int i = 0;i < source.Length;i++) {
            target[i] = source[i];
        }
    }
    catch (System.IndexOutOfRangeException) {
        // There are more columns in this row than
        // the original schema allows.  Stop copying
        // and return the DataRow.
        return target;
    }
    return target;
}

注解

ExecuteSelect调用 方法以从基础数据存储中检索数据并将其作为 IEnumerable 对象返回。 所有数据源控件都支持从其基础数据存储检索数据,即使不支持插入和排序等其他操作。 由于数据绑定控件可以随时由于事件或DataBind方法调用而DataSourceChanged请求数据列表,因此必须按需执行数据检索。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅