Bagikan melalui


DataSourceView Kelas

Definisi

Berfungsi sebagai kelas dasar untuk semua kelas tampilan sumber data, yang menentukan kemampuan kontrol sumber data.

public ref class DataSourceView abstract
public abstract class DataSourceView
type DataSourceView = class
Public MustInherit Class DataSourceView
Warisan
DataSourceView
Turunan

Contoh

Contoh kode berikut menunjukkan cara memperluas DataSourceView kelas untuk membuat kelas tampilan yang diketik dengan kuat untuk kontrol sumber data. Kelas CsVDataSourceView menentukan kemampuan CsvDataSource kontrol sumber data dan menyediakan implementasi untuk kontrol terikat data untuk menggunakan data yang disimpan dalam file nilai yang dipisahkan koma (.csv). Untuk informasi selengkapnya tentang CsvDataSource kontrol sumber data, lihat DataSourceControl kelas .

// The CsvDataSourceView class encapsulates the
// capabilities of the CsvDataSource data source control.
public class CsvDataSourceView : DataSourceView
{

    public CsvDataSourceView(IDataSource owner, string name) :base(owner, DefaultViewName) {
    }

    // The data source view is named. However, the CsvDataSource
    // only supports one view, so the name is ignored, and the
    // default name used instead.
    public static string DefaultViewName = "CommaSeparatedView";

    // The location of the .csv file.
    private string sourceFile = String.Empty;
    internal string SourceFile {
        get {
            return sourceFile;
        }
        set {
            // Use MapPath when the SourceFile is set, so that files local to the
            // current directory can be easily used.
            string mappedFileName = HttpContext.Current.Server.MapPath(value);
            sourceFile = mappedFileName;
        }
    }

    // Do not add the column names as a data row. Infer columns if the CSV file does
    // not include column names.
    private bool columns = false;
    internal bool IncludesColumnNames {
        get {
            return columns;
        }
        set {
            columns = value;
        }
    }

    // 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;
    }
    // The CsvDataSourceView does not currently
    // permit deletion. You can modify or extend
    // this sample to do so.
    public override bool CanDelete {
        get {
            return false;
        }
    }
    protected override int ExecuteDelete(IDictionary keys, IDictionary values)
    {
        throw new NotSupportedException();
    }
    // The CsvDataSourceView does not currently
    // permit insertion of a new record. You can
    // modify or extend this sample to do so.
    public override bool CanInsert {
        get {
            return false;
        }
    }
    protected override int ExecuteInsert(IDictionary values)
    {
        throw new NotSupportedException();
    }
    // The CsvDataSourceView does not currently
    // permit update operations. You can modify or
    // extend this sample to do so.
    public override bool CanUpdate {
        get {
            return false;
        }
    }
    protected override int ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues)
    {
        throw new NotSupportedException();
    }
}
' The CsvDataSourceView class encapsulates the
' capabilities of the CsvDataSource data source control.

Public Class CsvDataSourceView
   Inherits DataSourceView

   Public Sub New(owner As IDataSource, name As String)
       MyBase.New(owner, DefaultViewName)
   End Sub

   ' The data source view is named. However, the CsvDataSource
   ' only supports one view, so the name is ignored, and the
   ' default name used instead.
   Public Shared DefaultViewName As String = "CommaSeparatedView"

   ' The location of the .csv file.
   Private aSourceFile As String = [String].Empty

   Friend Property SourceFile() As String
      Get
         Return aSourceFile
      End Get
      Set
         ' Use MapPath when the SourceFile is set, so that files local to the
         ' current directory can be easily used.
         Dim mappedFileName As String
         mappedFileName = HttpContext.Current.Server.MapPath(value)
         aSourceFile = mappedFileName
      End Set
   End Property

   ' Do not add the column names as a data row. Infer columns if the CSV file does
   ' not include column names.
   Private columns As Boolean = False

   Friend Property IncludesColumnNames() As Boolean
      Get
         Return columns
      End Get
      Set
         columns = value
      End Set
   End Property

   ' 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

   ' The CsvDataSourceView does not currently
   ' permit deletion. You can modify or extend
   ' this sample to do so.
   Public Overrides ReadOnly Property CanDelete() As Boolean
      Get
         Return False
      End Get
   End Property

   Protected Overrides Function ExecuteDelete(keys As IDictionary, values As IDictionary) As Integer
      Throw New NotSupportedException()
   End Function 'ExecuteDelete

   ' The CsvDataSourceView does not currently
   ' permit insertion of a new record. You can
   ' modify or extend this sample to do so.
   Public Overrides ReadOnly Property CanInsert() As Boolean
      Get
         Return False
      End Get
   End Property

   Protected Overrides Function ExecuteInsert(values As IDictionary) As Integer
      Throw New NotSupportedException()
   End Function 'ExecuteInsert

   ' The CsvDataSourceView does not currently
   ' permit update operations. You can modify or
   ' extend this sample to do so.
   Public Overrides ReadOnly Property CanUpdate() As Boolean
      Get
         Return False
      End Get
   End Property

   Protected Overrides Function ExecuteUpdate(keys As IDictionary, _
                                              values As IDictionary, _
                                              oldValues As IDictionary) As Integer
      Throw New NotSupportedException()
   End Function 'ExecuteUpdate

End Class

Keterangan

ASP.NET mendukung arsitektur pengikatan data yang memungkinkan kontrol server Web untuk mengikat data secara konsisten. Kontrol server web yang mengikat data disebut sebagai kontrol terikat data, dan kelas yang memfasilitasi pengikatan disebut kontrol sumber data. Kontrol sumber data dapat mewakili sumber data apa pun: database relasional, file, aliran, objek bisnis, dan sebagainya. Kontrol sumber data menyajikan data dengan cara yang konsisten untuk kontrol terikat data, terlepas dari sumber atau format data yang mendasar.

Anda menggunakan kontrol sumber data yang disediakan dengan ASP.NET, termasuk SqlDataSource, , AccessDataSourcedan XmlDataSource, untuk melakukan sebagian besar tugas pengembangan Web. Anda menggunakan dasar DataSourceControl dan DataSourceView kelas saat ingin menerapkan kontrol sumber data kustom Anda sendiri.

Anda dapat menganggap kontrol sumber data sebagai kombinasi IDataSource objek dan daftar data terkait, yang disebut tampilan sumber data. Setiap daftar data diwakili oleh DataSourceView objek. Kelas DataSourceView adalah kelas dasar untuk semua tampilan sumber data, atau daftar data, yang terkait dengan kontrol sumber data. Tampilan sumber data menentukan kemampuan kontrol sumber data. Karena penyimpanan data yang mendasar berisi satu atau beberapa daftar data, kontrol sumber data selalu dikaitkan dengan satu atau beberapa tampilan sumber data bernama. Kontrol sumber data menggunakan GetViewNames metode untuk menghitung tampilan sumber data yang saat ini terkait dengan kontrol sumber data dan GetView metode untuk mengambil instans tampilan sumber data tertentu berdasarkan nama.

Semua DataSourceView objek mendukung pengambilan data dari sumber data yang mendasar menggunakan metode .ExecuteSelect Semua tampilan secara opsional mendukung serangkaian operasi dasar, termasuk operasi seperti ExecuteInsert, , ExecuteUpdatedan ExecuteDelete. Kontrol terikat data dapat menemukan kemampuan kontrol sumber data dengan mengambil tampilan sumber data terkait menggunakan GetView metode dan GetViewNames , dan dengan mengkueri tampilan pada waktu desain atau durasi.

Konstruktor

DataSourceView(IDataSource, String)

Menginisialisasi instans baru kelas DataSourceView.

Properti

CanDelete

Mendapatkan nilai yang menunjukkan apakah objek yang DataSourceView terkait dengan objek saat ini DataSourceControl mendukung ExecuteDelete(IDictionary, IDictionary) operasi.

CanInsert

Mendapatkan nilai yang menunjukkan apakah objek yang DataSourceView terkait dengan objek saat ini DataSourceControl mendukung ExecuteInsert(IDictionary) operasi.

CanPage

Mendapatkan nilai yang menunjukkan apakah objek yang DataSourceView terkait dengan objek saat ini DataSourceControl mendukung paging melalui data yang ExecuteSelect(DataSourceSelectArguments) diambil oleh metode .

CanRetrieveTotalRowCount

Mendapatkan nilai yang menunjukkan apakah objek yang DataSourceView terkait dengan objek saat ini DataSourceControl mendukung pengambilan jumlah total baris data, bukan data.

CanSort

Mendapatkan nilai yang menunjukkan apakah objek yang DataSourceView terkait dengan objek saat ini DataSourceControl mendukung tampilan yang diurutkan pada sumber data yang mendasar.

CanUpdate

Mendapatkan nilai yang menunjukkan apakah objek yang DataSourceView terkait dengan objek saat ini DataSourceControl mendukung ExecuteUpdate(IDictionary, IDictionary, IDictionary) operasi.

Events

Mendapatkan daftar delegasi penanganan aktivitas untuk tampilan sumber data.

Name

Mendapatkan nama tampilan sumber data.

Metode

CanExecute(String)

Menentukan apakah perintah yang ditentukan dapat dijalankan.

Delete(IDictionary, IDictionary, DataSourceViewOperationCallback)

Melakukan operasi penghapusan asinkron pada daftar data yang diwakili DataSourceView objek.

Equals(Object)

Menentukan apakah objek yang ditentukan sama dengan objek saat ini.

(Diperoleh dari Object)
ExecuteCommand(String, IDictionary, IDictionary)

Menjalankan perintah yang ditentukan.

ExecuteCommand(String, IDictionary, IDictionary, DataSourceViewOperationCallback)

Menjalankan perintah yang ditentukan.

ExecuteDelete(IDictionary, IDictionary)

Melakukan operasi penghapusan pada daftar data yang diwakili DataSourceView objek.

ExecuteInsert(IDictionary)

Melakukan operasi sisipkan pada daftar data yang diwakili DataSourceView objek.

ExecuteSelect(DataSourceSelectArguments)

Mendapatkan daftar data dari penyimpanan data yang mendasar.

ExecuteUpdate(IDictionary, IDictionary, IDictionary)

Melakukan operasi pembaruan pada daftar data yang diwakili DataSourceView objek.

GetHashCode()

Berfungsi sebagai fungsi hash default.

(Diperoleh dari Object)
GetType()

Mendapatkan instans Type saat ini.

(Diperoleh dari Object)
Insert(IDictionary, DataSourceViewOperationCallback)

Melakukan operasi penyisipan asinkron pada daftar data yang diwakili DataSourceView objek.

MemberwiseClone()

Membuat salinan dangkal dari yang saat ini Object.

(Diperoleh dari Object)
OnDataSourceViewChanged(EventArgs)

Memunculkan kejadian DataSourceViewChanged.

RaiseUnsupportedCapabilityError(DataSourceCapabilities)

Dipanggil oleh RaiseUnsupportedCapabilitiesError(DataSourceView) metode untuk membandingkan kemampuan yang ExecuteSelect(DataSourceSelectArguments) diminta untuk operasi dengan yang didukung tampilan.

Select(DataSourceSelectArguments, DataSourceViewSelectCallback)

Mendapatkan daftar data secara asinkron dari penyimpanan data yang mendasar.

ToString()

Mengembalikan string yang mewakili objek saat ini.

(Diperoleh dari Object)
Update(IDictionary, IDictionary, IDictionary, DataSourceViewOperationCallback)

Melakukan operasi pembaruan asinkron pada daftar data yang diwakili DataSourceView objek.

Acara

DataSourceViewChanged

Terjadi ketika tampilan sumber data telah berubah.

Berlaku untuk

Lihat juga