.Nettiers的事件:下拉控件数据绑定,Insert,调用自定存储过程,获取新增ID,事务,DeepLoad,DeepSave,Save,Update,DeepDelete
.Nettiers的事件:下拉控件数据绑定,Insert,调用自定存储过程,获取新增ID,事务,DeepLoad,DeepSave,Save,Update,DeepDelete.只用了.nettiers 的Data,Entites,Service层,没有用Web层
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Order.aspx.cs" Inherits="Order" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<table border="0" cellpadding="3" cellspacing="1" style="width: 480px">
<tr>
<td class="literal"><asp:Label ID="lbldataCustomerId" runat="server" Text="Customer Id:" AssociatedControlID="dataCustomerId" /></td>
<td style="width: 342px">
<asp:DropDownList ID="dataCustomerId" runat="server" Width="162px">
</asp:DropDownList></td>
</tr>
<tr>
<td class="literal"><asp:Label ID="lbldataEmployeeId" runat="server" Text="Employee Id:" AssociatedControlID="dataEmployeeId" /></td>
<td style="width: 342px">
<asp:DropDownList ID="dataEmployeeId" runat="server" Width="161px">
</asp:DropDownList></td>
</tr>
<tr>
<td class="literal"><asp:Label ID="lbldataOrderDate" runat="server" Text="Order Date:" AssociatedControlID="dataOrderDate" /></td>
<td style="width: 342px">
<asp:TextBox runat="server" ID="dataOrderDate" Text='<%# Bind("OrderDate", "{0:d}") %>' MaxLength="10"></asp:TextBox><asp:ImageButton ID="cal_dataOrderDate" runat="server" SkinID="CalendarImageButton" OnClientClick="javascript:return false;" /><ajaxToolkit:CalendarExtender runat="server" TargetControlID="dataOrderDate" CssClass="ajaxToolkit-CalendarExtender" Format="MM/dd/yyyy" PopupButtonID="cal_dataOrderDate" id="calext_dataOrderDate" />
</td>
</tr>
<tr>
<td class="literal"><asp:Label ID="lbldataRequiredDate" runat="server" Text="Required Date:" AssociatedControlID="dataRequiredDate" /></td>
<td style="width: 342px">
<asp:TextBox runat="server" ID="dataRequiredDate" Text='<%# Bind("RequiredDate", "{0:d}") %>' MaxLength="10"></asp:TextBox><asp:ImageButton ID="cal_dataRequiredDate" runat="server" SkinID="CalendarImageButton" OnClientClick="javascript:return false;" /><ajaxToolkit:CalendarExtender runat="server" TargetControlID="dataRequiredDate" CssClass="ajaxToolkit-CalendarExtender" Format="MM/dd/yyyy" PopupButtonID="cal_dataRequiredDate" id="calext_dataRequiredDate" />
</td>
</tr>
<tr>
<td class="literal"><asp:Label ID="lbldataShippedDate" runat="server" Text="Shipped Date:" AssociatedControlID="dataShippedDate" /></td>
<td style="width: 342px">
<asp:TextBox runat="server" ID="dataShippedDate" Text='<%# Bind("ShippedDate", "{0:d}") %>' MaxLength="10"></asp:TextBox><asp:ImageButton ID="cal_dataShippedDate" runat="server" SkinID="CalendarImageButton" OnClientClick="javascript:return false;" /><ajaxToolkit:CalendarExtender runat="server" TargetControlID="dataShippedDate" CssClass="ajaxToolkit-CalendarExtender" Format="MM/dd/yyyy" PopupButtonID="cal_dataShippedDate" id="calext_dataShippedDate" />
</td>
</tr>
<tr>
<td class="literal"><asp:Label ID="lbldataShipVia" runat="server" Text="Ship Via:" AssociatedControlID="dataShipVia" /></td>
<td style="width: 342px">
<asp:DropDownList ID="dataShipVia" runat="server" Width="155px">
</asp:DropDownList></td>
</tr>
<tr>
<td class="literal"><asp:Label ID="lbldataFreight" runat="server" Text="Freight:" AssociatedControlID="dataFreight" /></td>
<td style="width: 342px">
<asp:TextBox runat="server" ID="dataFreight" Text='<%# Bind("Freight") %>'></asp:TextBox><asp:RegularExpressionValidator ID="RegExVal_dataFreight" runat="server" ControlToValidate="dataFreight" Display="Dynamic" ValidationExpression="^[-]?(\d{1,9})(?:[.,]\d{1,4})?$" ErrorMessage="Invalid Value" />
</td>
</tr>
<tr>
<td class="literal"><asp:Label ID="lbldataShipName" runat="server" Text="Ship Name:" AssociatedControlID="dataShipName" /></td>
<td style="width: 342px">
<asp:TextBox runat="server" ID="dataShipName" Text='<%# Bind("ShipName") %>' MaxLength="40"></asp:TextBox>
</td>
</tr>
<tr>
<td class="literal"><asp:Label ID="lbldataShipAddress" runat="server" Text="Ship Address:" AssociatedControlID="dataShipAddress" /></td>
<td style="width: 342px">
<asp:TextBox runat="server" ID="dataShipAddress" Text='<%# Bind("ShipAddress") %>' MaxLength="60"></asp:TextBox>
</td>
</tr>
<tr>
<td class="literal"><asp:Label ID="lbldataShipCity" runat="server" Text="Ship City:" AssociatedControlID="dataShipCity" /></td>
<td style="width: 342px">
<asp:TextBox runat="server" ID="dataShipCity" Text='<%# Bind("ShipCity") %>' MaxLength="15"></asp:TextBox>
</td>
</tr>
<tr>
<td class="literal"><asp:Label ID="lbldataShipRegion" runat="server" Text="Ship Region:" AssociatedControlID="dataShipRegion" /></td>
<td style="width: 342px">
<asp:TextBox runat="server" ID="dataShipRegion" Text='<%# Bind("ShipRegion") %>' MaxLength="15"></asp:TextBox>
</td>
</tr>
<tr>
<td class="literal"><asp:Label ID="lbldataShipPostalCode" runat="server" Text="Ship Postal Code:" AssociatedControlID="dataShipPostalCode" /></td>
<td style="width: 342px">
<asp:TextBox runat="server" ID="dataShipPostalCode" Text='<%# Bind("ShipPostalCode") %>' MaxLength="10"></asp:TextBox>
</td>
</tr>
<tr>
<td class="literal"><asp:Label ID="lbldataShipCountry" runat="server" Text="Ship Country:" AssociatedControlID="dataShipCountry" /></td>
<td style="width: 342px">
<asp:TextBox runat="server" ID="dataShipCountry" Text='<%# Bind("ShipCountry") %>' MaxLength="15"></asp:TextBox>
</td>
</tr>
</table>
<asp:Button ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" OnClick="InsertButton_Click" />
<asp:Button ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" OnClick="UpdateButton_Click" />
<asp:Button ID="CancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
<asp:Button ID="DeepSaveButton" runat="server" CausesValidation="True" CommandName="DeepSave" Text="DeepSave" OnClick="DeepSaveButton_Click" />
<asp:Button ID="DeepLoadButton" runat="server" CausesValidation="True" CommandName="DeepLoad" Text="DeepLoad" OnClick="DeepLoadButton_Click" />
<asp:Button ID="BtnSave" runat="server" Text="Save" OnClick="BtnSave_Click" />
<asp:Button ID="BtnDeepDelete" runat="server" Text="DeepDelete" OnClick="BtnDeepDelete_Click" />
<asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label><br />
<br />
<table border="0" cellpadding="3" cellspacing="1">
<tr>
<td class="literal">
<asp:Label ID="lbldataProductId" runat="server" AssociatedControlID="dataProductId"
Text="Product Id:"></asp:Label></td>
<td>
<asp:TextBox ID="dataProductId" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td class="literal">
<asp:Label ID="lbldataUnitPrice" runat="server" AssociatedControlID="dataUnitPrice"
Text="Unit Price:"></asp:Label></td>
<td>
<asp:TextBox ID="dataUnitPrice" runat="server" Text='<%# Bind("UnitPrice") %>'></asp:TextBox><asp:RequiredFieldValidator
ID="ReqVal_dataUnitPrice" runat="server" ControlToValidate="dataUnitPrice" Display="Dynamic"
ErrorMessage="Required"></asp:RequiredFieldValidator><asp:RegularExpressionValidator
ID="RegExVal_dataUnitPrice" runat="server" ControlToValidate="dataUnitPrice"
Display="Dynamic" ErrorMessage="Invalid Value" ValidationExpression="^[-]?(\d{1,9})(?:[.,]\d{1,4})?$"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="literal">
<asp:Label ID="lbldataQuantity" runat="server" AssociatedControlID="dataQuantity"
Text="Quantity:"></asp:Label></td>
<td>
<asp:TextBox ID="dataQuantity" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox><asp:RequiredFieldValidator
ID="ReqVal_dataQuantity" runat="server" ControlToValidate="dataQuantity" Display="Dynamic"
ErrorMessage="Required"></asp:RequiredFieldValidator><asp:RangeValidator ID="RangeVal_dataQuantity"
runat="server" ControlToValidate="dataQuantity" Display="Dynamic" ErrorMessage="Invalid value"
MaximumValue="32767" MinimumValue="-32768" Type="Integer"></asp:RangeValidator>
</td>
</tr>
<tr>
<td class="literal">
<asp:Label ID="lbldataDiscount" runat="server" AssociatedControlID="dataDiscount"
Text="Discount:"></asp:Label></td>
<td>
<asp:TextBox ID="dataDiscount" runat="server" Text='<%# Bind("Discount") %>'></asp:TextBox><asp:RequiredFieldValidator
ID="ReqVal_dataDiscount" runat="server" ControlToValidate="dataDiscount" Display="Dynamic"
ErrorMessage="Required"></asp:RequiredFieldValidator><asp:RangeValidator ID="RangeVal_dataDiscount"
runat="server" ControlToValidate="dataDiscount" Display="Dynamic" ErrorMessage="Invalid value"
MaximumValue="999999999" MinimumValue="-999999999" Type="Double"></asp:RangeValidator>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
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;
using Northwind.Data;
using Northwind.Entities;
using Northwind.Services;
public partial class Order : System.Web.UI.Page
{
OrdersService orderService = new OrdersService();
CustomersService customersService = new CustomersService();
EmployeesService employeesService = new EmployeesService();
ShippersService shippersService = new ShippersService();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
/*下拉控件数据绑定*/#region /*下拉控件数据绑定*/
TList<Customers> customers = customersService.GetAll();
dataCustomerId.DataSource = customers;
dataCustomerId.DataTextField = "CompanyName";
dataCustomerId.DataValueField = "CustomerId";
dataCustomerId.DataBind();
TList<Employees> employees = employeesService.GetAll();
dataEmployeeId.DataSource = employees;
dataEmployeeId.DataTextField = "LastName";
dataEmployeeId.DataValueField = "EmployeeId";
dataEmployeeId.DataBind();
TList<Shippers> shippers = shippersService.GetAll();
dataShipVia.DataSource = shippers;
dataShipVia.DataTextField = "CompanyName";
dataShipVia.DataValueField = "ShipperId";
dataShipVia.DataBind();
this.Label1.Text = "";
#endregion
}
}
protected void InsertButton_Click(object sender, EventArgs e)
{
Insert();
}
public void Insert()
{
/**//*调用事务*/
TransactionManager tm = DataRepository.Provider.CreateTransaction();
tm.BeginTransaction();
try
{
/*调用Insert方法*/#region/*调用Insert方法*/
//插入数据到Orders表
OrdersService orderService = new OrdersService();
Orders orders = new Orders();
orders.CustomerId = dataCustomerId.SelectedValue;
orders.EmployeeId =Convert.ToInt32(dataEmployeeId.SelectedValue);
bool isok = orderService.Insert(orders); //更新一条
#endregion
/*调用自定义的存储过程***/#region/*调用自定义的存储过程***/
//获取Orders表的最大orderID
//OrdersService ordersService = new OrdersService();
//TList<Northwind.Entities.Orders> ds = ordersService.GetMaxOrderID();
//Response.Write(ds[0].OrderId);
//int orderId=ds[0].OrderId;
#endregion
数据库写的存储过程#region 数据库写的存储过程
/**//*CREATE PROCEDURE usp_Orders_GetMaxOrderID
AS
SELECT top 1
[OrderID],
[CustomerID],
[EmployeeID],
[OrderDate],
[RequiredDate],
[ShippedDate],
[ShipVia],
[Freight],
[ShipName],
[ShipAddress],
[ShipCity],
[ShipRegion],
[ShipPostalCode],
[ShipCountry]
FROM
[dbo].[Orders]
order by [OrderID] Desc
GO*/
#endregion
用以下方法获取新增的order的OrderID. (首选使用)#region 用以下方法获取新增的order的OrderID. (首选使用)
int orderId=orders.OrderId;
Response.Write(orderId);
#endregion
插入数据到OrdersDetail表#region 插入数据到OrdersDetail表
OrderDetailsService orderDetailsService = new OrderDetailsService();
OrderDetails orderDetails = new OrderDetails();
orderDetails.OrderId = orderId;
orderDetails.ProductId = Convert.ToInt32(dataProductId.Text);
orderDetails.Quantity = Convert.ToInt16(dataQuantity.Text);
orderDetails.UnitPrice = Convert.ToDecimal(dataUnitPrice.Text);
orderDetails.Discount = Convert.ToSingle(dataDiscount.Text);
bool isok2 = orderDetailsService.Insert(orderDetails); //更新一条
#endregion
if (isok&isok2)
{
this.Label1.Text = "succeed";
}
else
{
this.Label1.Text = "failed";
}
tm.Commit(); //提交事务
}
catch
{
tm.Rollback(); //回滚事务
throw;
}
}
public void DeepLoad(int OrdersId)
{
//TList<Orders> orders = DataRepository.OrdersProvider.GetAll(); //查找全部
//DataRepository.OrdersProvider.DeepLoad(orders, false, DeepLoadType.IncludeChildren, typeof(TList<OrderDetails>));
/**/////有11个重载 举一个最常用的, 第一个参数需要深度载入的对象,第二个是否递归,第三个参数选择载入类型(包含指定对象,忽略,不包含指定)这里是指定了需要载入的对象,后面是params type类型的参数,用于设置需要载入的类型 1对多用Tlist<类型> 多对1直接用类型
OrdersService orderService = new OrdersService();
Orders DeepLoadOrders = orderService.DeepLoadByOrderId(OrdersId, true, DeepLoadType.IncludeChildren, typeof(TList<OrderDetails>));
Response.Write(DeepLoadOrders);
Response.Write("<br>");
Response.Write(DeepLoadOrders.OrderDetailsCollection.GetList());
Response.Write("<br>");
Response.Write(DeepLoadOrders.OrderDetailsCollection.GetRange(1, 2)); //GetRange(int index,int count)第index条开始,提取count条
//Order order = Order.CreateOrder("ALFKI", 1, DateTime.Now, DateTime.Now,DateTime.Now, 1, 0.1m, "ship name", "ship address", "paris", "idf", "75000","france");
//order.OrderDetailCollection.Add(order.OrderID, 1, 15.6m, 10, 0.02f);
//order.OrderDetailCollection.Add(order.OrderID, 2, 122.6m, 43, 0.03f);
//DataRepository.OrderProvider.DeepSave(order);
//Console.WriteLine("new order saved: orderId is: " + order.OrderID.ToString());
//TList<Customers> customers = customersService.DeepSave();
}
protected void DeepLoadButton_Click(object sender, EventArgs e)
{
DeepLoad(10248);
}
protected void DeepSaveButton_Click(object sender, EventArgs e)
{
OrdersService orderService = new OrdersService();
Orders orders = orderService.GetByOrderId(11092); //
orders.CustomerId = dataCustomerId.SelectedValue;
orders.EmployeeId = Convert.ToInt32(dataEmployeeId.SelectedValue);
bool isok = orderService.DeepSave(orders); //更新一条
if (isok)
{
this.Label1.Text = "succeed";
}
else
{
this.Label1.Text = "failed";
}
}
protected void BtnSave_Click(object sender, EventArgs e)
{
/*调用Save方法*/ 单个Orders表#region/*调用Save方法*/ 单个Orders表
OrdersService orderService = new OrdersService();
Orders orders = orderService.GetByOrderId(11092); //11092为OrderID号
orders.CustomerId = dataCustomerId.SelectedValue;
orders.EmployeeId = Convert.ToInt32(dataEmployeeId.SelectedValue);
orderService.Save(orders); //更新一条
#endregion
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
/*调用Update方法*/ 单个Orders表#region/*调用Update方法*/ 单个Orders表
OrdersService orderService = new OrdersService();
Orders orders = orderService.GetByOrderId(11092); //11092为OrderID号
orders.CustomerId = dataCustomerId.SelectedValue;
orders.EmployeeId = Convert.ToInt32(dataEmployeeId.SelectedValue);
bool isok=orderService.Update(orders); //更新一条
if (isok)
{
this.Label1.Text = "succeed";
}
else
{
this.Label1.Text = "failed";
}
#endregion
}
protected void BtnDeepDelete_Click(object sender, EventArgs e)
{
例子#region 例子
//VERG_User user = DataRepository.VERG_UserProvider.GetbyId(id);
//DataRepository.VERG_UserProvider.Deepload(user, false, DeepLoadType.IncludeChildren, typeof(TList<VERG_ClientOnline>), typeof(TList<VERG_ContactGroup>), typeof(TList<VERG_ServiceState>));
//TransactionManager tm = DataRepository.Provider.CreateTransaction();
//tm.BeginTransaction();
//try
//{
// DataRepository.VERG_ClientOnlineProvider.Delete(tm, user.VERG_ClientOnlineCollection);
// DataRepository.VERG_ContactGroupProvider.Delete(tm, user.VERG_ContactGroupCollection);
// DataRepository.VERG_ServiceStateProvider.Delete(tm, user.VERG_ServiceStateCollection);
// DataRepository.VERG_UserProvider.Delete(tm, user);
// tm.Commit();
//}
//catch
//{
// tm.Rollback();
// throw;
//}
#endregion
Orders order = DataRepository.OrdersProvider.GetByOrderId(11093);
//DataRepository.OrdersProvider.DeepLoad(order, false, DeepLoadType.IncludeChildren, typeof(TList<OrderDetails>));
OrdersService orderService=new OrdersService();
Orders DeepLoadOrders = orderService.DeepLoadByOrderId(order.OrderId, false, DeepLoadType.IncludeChildren, typeof(TList<OrderDetails>));
TransactionManager tm = DataRepository.Provider.CreateTransaction();
tm.BeginTransaction();
try
{
DataRepository.OrderDetailsProvider.Delete(tm, DeepLoadOrders.OrderDetailsCollection);
// DataRepository.VERG_ContactGroupProvider.Delete(tm, user.VERG_ContactGroupCollection);
DataRepository.OrdersProvider.Delete(tm, order);
tm.Commit();
}
catch
{
tm.Rollback();
throw;
}
}
}