在在.NET3.5平台上使用平台上使用LINQtoSQL创建三层创建三层/多层多层Web应用系统应用系统
(二二)
使用使用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
{