.Nettiers的事件:下拉控件数据绑定,Insert,调用自定存储过程,获取新增ID,事务,DeepLoad,DeepSave,Save,Update,DeepDelete【原创】

.Nettiers的事件:下拉控件数据绑定,Insert,调用自定存储过程,获取新增ID,事务,DeepLoad,DeepSave,Save,Update,DeepDelete.只用了.nettiers 的Data,Entites,Service层,没有用Web层

 

Code

 

 

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)
        
{
            
/*下拉控件数据绑定*/
        }

    }


    
protected void InsertButton_Click(object sender, EventArgs e)
    
{
        Insert();
    }

    
public void Insert()
     
{


   
/*调用事务*/   
        TransactionManager tm 
= DataRepository.Provider.CreateTransaction();

         tm.BeginTransaction();

         
try
         
{

             
/*调用Insert方法*/

             
/*调用自定义的存储过程***/

             
数据库写的存储过程

             
用以下方法获取新增的order的OrderID.  (首选使用)

             
插入数据到OrdersDetail表

             
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(
12)); //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表
    }


    
protected void UpdateButton_Click(object sender, EventArgs e)
    
{
        
/*调用Update方法*/  单个Orders表
    }

    
protected void BtnDeepDelete_Click(object sender, EventArgs e)
    
{
        
例子

        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;
        }

    }

}


 

 

 

posted @ 2008-08-27 20:29  KiNg.JiOnG  阅读(1268)  评论(1编辑  收藏  举报
查看博客访问人数(点击):