指引网

当前位置: 主页 > 编程开发 > .NET >

用LINQtoSQL创建Web应用系统(三)

来源:网络 作者:佚名 点击: 时间:2017-11-17 03:45
[摘要] 使用LINQ实现数据访问层 数据访问层包含与Data Linq交互的代码,它使用集成语言查询来访问Data Linq层。下图2.1 展示了数据访问层的细节。基本上,它包括了所有与上层交互的方法,并完成与数据

        使用LINQ实现数据访问层

        数据访问层包含与Data Linq交互的代码,它使用集成语言查询来访问Data Linq层。下图2.1 展示了数据访问层的细节。基本上,它包括了所有与上层交互的方法,并完成与数据库相关的操作。       

        图2.1:数据访问层-详细视图

        在示例程序中,数据访问层包含了一个简单的组件DALCustomer,相关代码如下(http://www.EntLib.com 开源小组注: 这里相关的示例代码采用C#,原文为VB代码):

        代码片段1.1:数据访问层

             public class DALCustomer
            {
                private DBLinqDataContext objDataContext = new DBLinqDataContext();
                public Table<Customer> SelectRecordAll()
                {
                    try
                    {
                       return objDataContext.Customers;
                    }
                    catch(Exception ex)
                    {
                        throw ex;
                    }
                }
                public Customer SelectRecordByID(string customerID)
                {
                    try
                    {
                        return (from cust in objDataContext.Customers
                                where cust.CustomerID == customerID
                                select cust).Single();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                public List<Customer> SelectRecordByIDListable(string customerID)
                {
                    List<Customer> localTable;
                    try
                    {
                        localTable = (from cust in objDataContext.Customers
                                      where cust.CustomerID == customerID
                                      select cust).ToList();
                        return localTable;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                public string InsertRecord(Customer localTable)
                {
                    try
                    {
                        objDataContext.Customers.InsertOnSubmit(localTable);
                        objDataContext.SubmitChanges();
                        return localTable.CustomerID;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                public void UpdateRecord(Customer localTable)
                {
                    try
                    {
                        objDataContext.Customers.Attach(localTable);
                        objDataContext.Refresh(RefreshMode.KeepCurrentValues, localTable);
                        objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                public void DeleteRecord(string customerID)
                {
                    try
                    {
                        objDataContext.Customers.DeleteOnSubmit((from cust in objDataContext.Customers
                                                                where cust.CustomerID==customerID
                                                                     select cust).Single());
                        objDataContext.SubmitChanges();
                    }
                    catch(Exception ex)
                    {
                        throw ex;
                    }
                }
                public Table<Order> SelectAllOrder()
                {
                    try
                    {
                        return objDataContext.Orders;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                public Table<Order_Detail> SelectAllOrderDetail()
                {
                    try
                    {
                        return objDataContext.Order_Details;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }


------分隔线----------------------------