DataSourceView.ExecuteSelect(DataSourceSelectArguments) メソッド

定義

基になるデータ ストレージからデータのリストを取得します。Gets a list of data from the underlying data storage.

protected public:
 abstract System::Collections::IEnumerable ^ ExecuteSelect(System::Web::UI::DataSourceSelectArguments ^ arguments);
protected internal abstract System.Collections.IEnumerable ExecuteSelect (System.Web.UI.DataSourceSelectArguments arguments);
abstract member ExecuteSelect : System.Web.UI.DataSourceSelectArguments -> System.Collections.IEnumerable
Protected Friend MustOverride Function ExecuteSelect (arguments As DataSourceSelectArguments) As IEnumerable

パラメーター

arguments
DataSourceSelectArguments

基本的なデータ取得を超えたデータ操作を要求する場合に使用する DataSourceSelectArgumentsA DataSourceSelectArguments that is used to request operations on the data beyond basic data retrieval.

戻り値

基になるデータ ストレージのデータで構成される IEnumerable リスト。An IEnumerable list of data from the underlying data storage.

クラスDataSourceViewを拡張するクラスのExecuteSelectメソッドをオーバーライドする方法を次のコード例に示します。The following code example demonstrates how to override the ExecuteSelect method in a class that extends the DataSourceView class. CsvDataSourceView 、コンマ区切り値 (.csv) ファイルを開き、行ごとに解析し、 DataTableオブジェクトとDataView 、データをメモリに保持するオブジェクトを作成します。The CsvDataSourceView opens a comma-separated value (.csv) file, parses it line by line, and creates a DataTable object and a DataView object to hold the data in memory. 最後に、 DataSourceSelectArgumentsオブジェクトDataViewによって指定された並べ替え式が適用され、 IEnumerableオブジェクトがインスタンスとして返されます。Finally, a sort expression is applied if one is supplied by the DataSourceSelectArguments object, and the DataView object is returned as an IEnumerable instance. このコード例は、 DataSourceViewクラスに対して提供される大規模な例の一部です。This code example is part of a larger example provided for the DataSourceView class.

// 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;
}
' Get data from the underlying data source.
' Build and return a DataView, regardless of mode.
Protected Overrides Function ExecuteSelect(selectArgs As DataSourceSelectArguments) _
 As System.Collections.IEnumerable
   Dim dataList As IEnumerable = Nothing
   ' Open the .csv file.
   If File.Exists(Me.SourceFile) Then
      Dim data As New DataTable()

      ' Open the file to read from.
      Dim sr As StreamReader = File.OpenText(Me.SourceFile)

      Try
         ' Parse the line
         Dim dataValues() As String
         Dim col As DataColumn

         ' Do the following to add schema.
         dataValues = sr.ReadLine().Split(","c)
         ' For each token in the comma-delimited string, add a column
         ' to the DataTable schema.
         Dim token As String
         For Each token In dataValues
            col = New DataColumn(token, System.Type.GetType("System.String"))
            data.Columns.Add(col)
         Next token

         ' Do not add the first row as data if the CSV file includes column names.
         If Not IncludesColumnNames Then
            data.Rows.Add(CopyRowData(dataValues, data.NewRow()))
         End If

         ' Do the following to add data.
         Dim s As String
         Do
            s = sr.ReadLine()
            If Not s Is Nothing Then
                dataValues = s.Split(","c)
                data.Rows.Add(CopyRowData(dataValues, data.NewRow()))
            End If
         Loop Until s Is Nothing

      Finally
         sr.Close()
      End Try

      data.AcceptChanges()
      Dim dataView As New DataView(data)
      If Not selectArgs.SortExpression Is String.Empty Then
          dataView.Sort = selectArgs.SortExpression
      End If
      dataList = dataView
   Else
      Throw New System.Configuration.ConfigurationErrorsException("File not found, " + Me.SourceFile)
   End If

   If dataList is Nothing Then
      Throw New InvalidOperationException("No data loaded from data source.")
   End If

   Return dataList
End Function 'ExecuteSelect


Private Function CopyRowData([source]() As String, target As DataRow) As DataRow
   Try
      Dim i As Integer
      For i = 0 To [source].Length - 1
         target(i) = [source](i)
      Next i
   Catch iore As IndexOutOfRangeException
      ' There are more columns in this row than
      ' the original schema allows.  Stop copying
      ' and return the DataRow.
      Return target
   End Try
   Return target
End Function 'CopyRowData

注釈

メソッドExecuteSelectは、基になるデータストアからデータを取得し、それをIEnumerableオブジェクトとして返すために呼び出されます。The ExecuteSelect method is called to retrieve data from the underlying data store and return it as an IEnumerable object. すべてのデータソースコントロールは、挿入や並べ替えなどの他の操作がサポートされていない場合でも、基になるデータストレージからのデータの取得をサポートします。All data source controls support data retrieval from their underlying data storage, even if other operations such as insertion and sorting are not supported. データバインドコントロールは、 DataSourceChangedイベントDataBindまたはメソッドの呼び出しの結果として、いつでもデータの一覧を要求できるため、データの取得は要求時に実行する必要があります。Because a data-bound control can request a list of data at any time as a result of a DataSourceChanged event or a DataBind method call, the data retrieval must be performed on demand.

適用対象

こちらもご覧ください