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 方法从基础数据源检索数据。 所有视图(可选)都支持一组基本操作,包括 、 ExecuteInsertExecuteUpdate和 ExecuteDelete等操作。 数据绑定控件可以使用 和 GetViewNames 方法检索关联的数据源视图GetView,并在设计时或运行时查询视图,从而发现数据源控件的功能。
构造函数
DataSourceView(IDataSource, String) |
初始化 DataSourceView 类的新实例。 |
属性
CanDelete |
获取一个值,该值指示与当前 DataSourceControl 对象关联的 DataSourceView 对象是否支持 ExecuteDelete(IDictionary, IDictionary) 操作。 |
CanInsert |
获取一个值,该值指示与当前 DataSourceControl 对象关联的 DataSourceView 对象是否支持 ExecuteInsert(IDictionary) 操作。 |
CanPage |
获取一个值,该值指示与当前 DataSourceControl 对象关联的 DataSourceView 对象是否支持对通过 ExecuteSelect(DataSourceSelectArguments) 方法检索到的数据进行分页。 |
CanRetrieveTotalRowCount |
获取一个值,该值指示与当前 DataSourceControl 对象关联的 DataSourceView 对象是否支持检索数据的总行数(而不是数据)。 |
CanSort |
获取一个值,该值指示与当前 DataSourceControl 对象关联的 DataSourceView 对象是否支持基础数据源的排序视图。 |
CanUpdate |
获取一个值,该值指示与当前 DataSourceControl 对象关联的 DataSourceView 对象是否支持 ExecuteUpdate(IDictionary, IDictionary, IDictionary) 操作。 |
Events |
获取数据源视图的事件处理程序委托的列表。 |
Name |
获取数据源视图的名称。 |
方法
事件
DataSourceViewChanged |
在数据源视图更改时发生。 |