当前位置:首页 > 温故知新ASP.NET 2.0(C#)(8) – DataSourceControl(数据源控件)

温故知新ASP.NET 2.0(C#)(8) – DataSourceControl(数据源控件)

点击次数:1078  更新日期:2010-12-29
\n

介绍
在 ASP.NET 2.0 中有几个新的数据源控件,例如,SqlDataSource、ObjectDataSource、XmlDataSource、AccessDataSource 和 SiteMapDataSource。它们全都可以用来从它们各自类型的数据源中检索数据,并且可以绑定到各种数据绑定控件。数据源控件减少了为检索和绑定数据甚至对数据进行排序、分页或编辑而需要编写的自定义代码的数量。


\n

其中 ObjectDataSource 控件可针对各种基础数据存储区(如 SQL 数据库或 XML)启用声明性数据绑定模型。因为页开发人员也常常将数据检索(也可能包含业务逻辑)封装在一个组件对象中,从而在呈现页和数据提供程序之间引入另一个层。ObjectDataSource 控件允许开发人员使用此传统的三层结构构造应用程序,同时仍然能够利用 ASP.NET 中的声明性数据绑定模型的易用性优点。


\n


关键
1、在数据层创建 强类型的DataSet和TableAdapter,TableAdapter查询可以使用现有的存储过程。注:直接把表或存储过程拖进来会自动创建TableAdapter


\n

2、中间层的类用[System.ComponentModel.DataObject]声明为数据组件,CRUD方法分别对应[DataObjectMethod(DataObjectMethodType.Insert)],[DataObjectMethod(DataObjectMethodType.Select)],[DataObjectMethod(DataObjectMethodType.Update)],[DataObjectMethod(DataObjectMethodType.Delete)]


\n

3、web层使用ObjectDataSource展现数据,ObjectDataSource就相当于一个代理。ObjectDataSource只是查找具有匹配的参数名称的方法,它不会使用参数的 Type 或 Size,而只是对参数的名称进行匹配


\n

4、其它
·<asp:Parameter />有ConvertEmptyStringToNull属性,默认是true。另外还有Direction属性
·注意<asp:BoundField />的这几个属性NullDisplayText,HtmlEncode,ApplyFormatInEditMode,InsertVisible,DataFormatString,ReadOnly
·DataKeyNames有多个值的话用“,”分隔,用<asp:ControlParameter />绑定的话给其加一个“PropertyName”属性,值类似如下“SelectedDataKey.Values[0]”
·关于绑定:简单属性<%# custID %>;集合<asp:ListBox id=”List1″ datasource=’<%# myArray %>’ runat=”server”>;表达式<%# ( customer.FirstName + ” ” + customer.LastName ) %>;方法<%# GetBalance(custID) %>
·<%# Eval(“field1″) %> 和 <%# Bind(“field1″) %>,Eval是单向绑定,Bind是双向邦定
·<asp:ObjectDataSource />有一个OldValuesParameterFormatString属性一般不用,不过如果要处理乐观并发之类的就会用到。当该属性的值为“original_{0}”的时候“original_参数名”则为初始值
·还有一些如编程方式给参数赋值,错误处理,得到返回值之类的请看源码


\n


示例
相关存储过程和数据层略,见源码


\n

单例模式的实现
App_Code/Singleton.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


\n

/**//// <summary>
/// Singleton 的摘要说明
/// </summary>
public class Singleton<T> where T : new()
{
public static T Instance
{
get { return SingletonCreator.instance; }
}


\n

class SingletonCreator
{
internal static readonly T instance = new T();
}
}


\n

中间层代码
App_Code/Test.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


\n

using TestDatabaseTableAdapters;
using System.ComponentModel;


\n

/**//// <summary>
/// Test 的摘要说明
/// </summary>
[DataObject]
public class Test
{
[DataObjectMethod(DataObjectMethodType.Select, true)]
public TestDatabase.TestDataTable GetTest()
{
return Singleton<TestTableAdapter>.Instance.GetTest();
}


\n

[DataObjectMethod(DataObjectMethodType.Select, false)]
public TestDatabase.TestDataTable GetTestById(int id)
{
return Singleton<TestTableAdapter>.Instance.GetTestById(id);
}


\n

[DataObjectMethod(DataObjectMethodType.Insert, true)]
public int?[] InsertTest(int? parentId, string name, DateTime? publishTime, decimal? price, bool? isGood, out int? minId)
{
// 仅为说明如何做错误处理
if (String.IsNullOrEmpty(name))
throw new ArgumentException(“参数不能是空”, “name”);


\n

int? id = null;
int? count = null;
minId = null;


\n

Singleton<TestTableAdapter>.Instance.InsertTest(parentId, name, publishTime, price, isGood, ref id, ref count, ref minId);
return new int?[] { id, count };
}


\n

[DataObjectMethod(DataObjectMethodType.Delete, true)]
public int? DeleteTest(int id)
{
int? rowAffected;


\n

rowAffected = Singleton<TestTableAdapter>.Instance.DeleteTest(id);
return rowAffected;
}


\n

[DataObjectMethod(DataObjectMethodType.Update, true)]
public int? UpdateTest(int? id, int? parentId, string name, DateTime? publishTime, decimal? price, bool? isGood)
{
int? rowAffected;


\n

rowAffected = Singleton<TestTableAdapter>.Instance.UpdateTest(id, parentId, name, publishTime, price, isGood);
return rowAffected;
}
}


\n

DataSourceControl/Test.aspx
<%@ Page Language=”C#” MasterPageFile=”~/Site.master” AutoEventWireup=”true” CodeFile=”Test.aspx.cs”
Inherits=”DataSourceControl_Test” Title=”数据源控件测试” %>


\n

<asp:Content ID=”Content1″ ContentPlaceHolderID=”ContentPlaceHolder1″ runat=”Server”>
<p>
<asp:Label ID=”lblMsg” runat=”server” ForeColor=”red” />
</p>
<table cellpadding=”6″>
<tr>
<td valign=”top”>
<asp:DetailsView ID=”DetailsView1″ runat=”server” AllowPaging=”True” AutoGenerateRows=”False”
DataKeyNames=”Id” DataSourceID=”ObjectDataSource1″ Height=”50px” Width=”125px” OnItemInserted=”DetailsView1_ItemInserted”>
<Fields>
<asp:BoundField DataField=”Id” HeaderText=”Id” InsertVisible=”False” ReadOnly=”True”
SortExpression=”Id” />
<asp:BoundField DataField=”ParentId” HeaderText=”ParentId” SortExpression=”ParentId” />
<asp:BoundField DataField=”Name” HeaderText=”Name” SortExpression=”Name” />
<asp:BoundField DataField=”PublishTime” HeaderText=”PublishTime” SortExpression=”PublishTime”
InsertVisible=”False” />
<asp:BoundField DataField=”Price” HeaderText=”Price” SortExpression=”Price” DataFormatString=”{0:c}” HtmlEncode=”False” />
<asp:CheckBoxField DataField=”IsGood” HeaderText=”IsGood” SortExpression=”IsGood” />
<asp:CommandField ShowInsertButton=”True” />
</Fields>
</asp:DetailsView>
</td>
<td valign=”top”>
<asp:DetailsView ID=”DetailsView2″ runat=”server” Height=”50px” Width=”125px” AutoGenerateRows=”False” DataKeyNames=”Id” DataSourceID=”ObjectDataSource2″>
<Fields>
<asp:BoundField DataField=”Id” HeaderText=”Id” InsertVisible=”False” ReadOnly=”True”
SortExpression=”Id” />
<asp:BoundField DataField=”ParentId” HeaderText=”ParentId” SortExpression=”ParentId” />
<asp:BoundField DataField=”Name” HeaderText=”Name” SortExpression=”Name” />
<asp:BoundField DataField=”PublishTime” HeaderText=”PublishTime” SortExpression=”PublishTime” />
<asp:BoundField DataField=”Price” HeaderText=”Price” SortExpression=”Price” DataFormatString=”{0:c}” HtmlEncode=”false” />
<asp:CheckBoxField DataField=”IsGood” HeaderText=”IsGood” SortExpression=”IsGood” />
</Fields>
</asp:DetailsView>
</td>
</tr>
</table>
<p>
<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” DataKeyNames=”Id”
DataSourceID=”ObjectDataSource1″ AllowPaging=”True” AllowSorting=”True” OnRowUpdating=”GridView1_RowUpdating”>
<Columns>
<asp:CommandField ShowDeleteButton=”True” ShowEditButton=”True” ShowSelectButton=”True” />
<asp:BoundField DataField=”Id” HeaderText=”Id” InsertVisible=”False” ReadOnly=”True”
SortExpression=”Id” />
<asp:BoundField DataField=”ParentId” HeaderText=”ParentId” SortExpression=”ParentId”
NullDisplayText=”我的值是NULL” />
<asp:BoundField DataField=”Name” HeaderText=”Name” SortExpression=”Name” />
<asp:BoundField DataField=”PublishTime” HeaderText=”PublishTime” SortExpression=”PublishTime”
ReadOnly=”true” />
<asp:BoundField DataField=”Price” HeaderText=”Price” SortExpression=”Price” DataFormatString=”{0:c}” HtmlEncode=”false” ApplyFormatInEditMode=”True” />
<asp:CheckBoxField DataField=”IsGood” HeaderText=”IsGood” SortExpression=”IsGood” />
</Columns>
</asp:GridView>
</p>
<asp:ObjectDataSource ID=”ObjectDataSource1″ runat=”server” SelectMethod=”GetTest”
TypeName=”Test” InsertMethod=”InsertTest” DeleteMethod=”DeleteTest” UpdateMethod=”UpdateTest”
OnInserting=”ObjectDataSource1_Inserting” OnInserted=”ObjectDataSource1_Inserted”>
<InsertParameters>
<%–ConvertEmptyStringToNull属性默认为true–%>
<asp:Parameter Name=”parentId” Type=”Int32″ ConvertEmptyStringToNull=”true” />
<asp:Parameter Name=”name” Type=”String” />
<asp:Parameter Name=”publishTime” Type=”DateTime” />
<asp:Parameter Name=”price” Type=”Decimal” />
<asp:Parameter Name=”isGood” Type=”Boolean” />
<asp:Parameter Direction=”Output” Name=”minId” Type=”Int32″ />
</InsertParameters>
<DeleteParameters>
<asp:Parameter Name=”id” Type=”Int32″ />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name=”id” Type=”Int32″ />
<asp:Parameter Name=”parentId” Type=”Int32″ />
<asp:Parameter Name=”name” Type=”String” />
<asp:Parameter Name=”publishTime” Type=”DateTime” />
<asp:Parameter Name=”price” Type=”Decimal” />
<asp:Parameter Name=”isGood” Type=”Boolean” />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID=”ObjectDataSource2″ runat=”server” OldValuesParameterFormatString=”original_{0}”
SelectMethod=”GetTestById” TypeName=”Test”>
<SelectParameters>
<asp:ControlParameter ControlID=”GridView1″ Name=”id” PropertyName=”SelectedValue”
Type=”Int32″ />
</SelectParameters>
</asp:ObjectDataSource>
</asp:Content>


\n

DataSourceControl/Test.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


\n

public partial class DataSourceControl_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


\n

}


\n

protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
// 编程方式给参数赋值
if (e.InputParameters["publishTime"] == null)
{
e.InputParameters["publishTime"] = DateTime.Now;
}
}


\n

protected void ObjectDataSource1_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
// 错误处理
if (e.Exception != null)
{
if (e.Exception.InnerException != null)
{
Exception inner = e.Exception.InnerException;


\n

if (inner is ArgumentException)
{
string paramName = ((ArgumentException)inner).ParamName;
lblMsg.Text = string.Concat(“参数 “, paramName, ” 有问题”);
}
}
}
else
{
int?[] ary = (int?[])e.ReturnValue;
lblMsg.Text = “新插入信息的ID是:” + ary[0].ToString();
lblMsg.Text += “<br />数据总数是:” + ary[1].ToString();


\n

lblMsg.Text += “<br />最小ID是:” + e.OutputParameters["minId"].ToString();
}
}


\n

protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
if (e.Exception != null)
{
// 错误已处理
e.ExceptionHandled = true;
// DetailsView保持插入状态
e.KeepInInsertMode = true;
}
}


\n

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// 价格格式转换为decimal格式
if (e.NewValues["Price"] != null)
e.NewValues["Price"] = decimal.Parse(e.NewValues["Price"].ToString(), System.Globalization.NumberStyles.Currency);
}
}


[源码下载]


作者:webabcd

\n