DataSourceView 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當做所有資料來源檢視類別的基底類別,這些類別會定義資料來源控制項的功能。
public ref class DataSourceView abstract
public abstract class DataSourceView
type DataSourceView = class
Public MustInherit Class DataSourceView
- 繼承
-
DataSourceView
- 衍生
範例
下列程式碼範例示範如何擴充 DataSourceView 類別,以建立資料來源控制項的強型別檢視類別。 類別 CsVDataSourceView
會定義資料來源控制項的功能 CsvDataSource
,並提供資料繫結控制項的實作,以使用儲存在逗號分隔值 (.csv) 檔案中的資料。 如需資料來源控制項的詳細資訊 CsvDataSource
,請參閱 類別 DataSourceControl 。
// 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
備註
ASP.NET 支援資料系結架構,可讓 Web 服務器控制項以一致的方式系結至資料。 系結至資料的 Web 服務器控制項稱為資料繫結控制項,以及有助於系結的類別稱為資料來源控制項。 資料來源控制項可以代表任何資料來源:關係資料庫、檔案、資料流程、商務物件等等。 資料來源控制項會以一致的方式呈現資料給資料繫結控制項,而不論基礎資料的來源或格式為何。
您可以使用提供給 ASP.NET 的資料來源控制項,包括 SqlDataSource 、 AccessDataSource 和 XmlDataSource ,來執行大部分的 Web 開發工作。 當您想要實作自己的自訂資料來源控制項時,請使用基底 DataSourceControl 和 DataSourceView 類別。
您可以將資料來源控制項視為物件及其相關聯資料清單的組合 IDataSource ,稱為資料來源檢視。 每個資料清單都是以 DataSourceView 物件表示。 類別 DataSourceView 是與資料來源控制項相關聯的所有資料來源檢視或資料清單的基類。 資料來源檢視會定義資料來源控制項的功能。 因為基礎資料儲存體包含一或多個資料清單,所以資料來源控制項一律會與一或多個具名資料來源檢視相關聯。 資料來源控制項會 GetViewNames 使用 方法來列舉目前與資料來源控制項相關聯的資料來源檢視,以及 GetView 依名稱擷取特定資料來源檢視實例的方法。
所有 DataSourceView 物件都支援使用 ExecuteSelect 方法從基礎資料來源擷取資料。 所有檢視選擇性地支援一組基本作業,包括 、 ExecuteUpdate 和 ExecuteDelete 等 ExecuteInsert 作業。 資料繫結控制項可以使用 和 GetViewNames 方法來擷取相關聯的資料來源檢視,以及在設計階段或執行時間查詢檢視 GetView ,來探索資料來源控制項的功能。
建構函式
DataSourceView(IDataSource, String) |
初始化 DataSourceView 類別的新執行個體。 |
屬性
CanDelete |
取得值,指出與目前 DataSourceView 物件關聯的 DataSourceControl 物件是否支援 ExecuteDelete(IDictionary, IDictionary) 作業。 |
CanInsert |
取得值,指出與目前 DataSourceView 物件關聯的 DataSourceControl 物件是否支援 ExecuteInsert(IDictionary) 作業。 |
CanPage |
取得值,指出與目前 DataSourceView 物件關聯的 DataSourceControl 物件是否支援對 ExecuteSelect(DataSourceSelectArguments) 方法擷取的資料進行分頁。 |
CanRetrieveTotalRowCount |
取得值,指出與目前 DataSourceView 物件關聯的 DataSourceControl 物件是否支援擷取資料列總數,而非資料。 |
CanSort |
取得值,指出與目前 DataSourceView 物件關聯的 DataSourceControl 物件是否支援對基礎資料來源的排序檢視。 |
CanUpdate |
取得值,指出與目前 DataSourceView 物件關聯的 DataSourceControl 物件是否支援 ExecuteUpdate(IDictionary, IDictionary, IDictionary) 作業。 |
Events |
取得資料來源檢視的事件處理常式委派清單。 |
Name |
取得資料來源檢視的名稱。 |
方法
事件
DataSourceViewChanged |
當資料來源檢視已變更時發生。 |