linq to Sql 增删改查

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
public class DALTemplateServer
{
/// <summary>
/// 添加模版方法
/// </summary>
/// <param name="template">模版对象</param>
public void add(Model.t_template template)
{
using (Model.NewDataContext dataContext = new Model.NewDataContext())
{
dataContext.t_template.InsertOnSubmit(template);
dataContext.SubmitChanges();
}
}
/// <summary>
/// 删除模版的方法
/// </summary>
/// <param name="asf">委托</param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <returns></returns>
public void delete(Func<Model.t_template, bool> asf)
{
using (Model.NewDataContext dataContext = new Model.NewDataContext())
{
try
{
var objTemp
= dataContext.t_template.SingleOrDefault(asf);
dataContext.t_template.DeleteOnSubmit(objTemp);
dataContext.SubmitChanges();
}
catch (ArgumentException e)
{
throw e;
}
catch (InvalidOperationException e)
{

throw e;
}
}
}
/// <summary>
/// 更新模版方法
/// </summary>
/// <exception cref="ArgumentException"></exception>
/// <param name="template">模版对象</param>
public void update(Model.t_template template)
{
using (Model.NewDataContext dataContext = new Model.NewDataContext())
{
Model.t_template temp
= null;
try
{
temp
= dataContext.t_template.Where(p => p.id == template.id).Single();
}
catch (ArgumentException e)
{

throw e;
}

temp.name
= template.name;
temp.url
= template.url;
temp.inputDate
= template.inputDate;

dataContext.SubmitChanges();
}
}
/// <summary>
/// 查询模版
/// </summary>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <param name="asf"></param>
/// <returns></returns>
public Model.t_template find(Func<Model.t_template, bool> asf)
{
using (Model.NewDataContext dataContext = new Model.NewDataContext())
{
try
{
return dataContext.t_template.SingleOrDefault(asf);
}
catch (ArgumentException e)
{
throw e;
}
catch (InvalidOperationException e)
{

throw e;
}
}
}
/// <summary>
/// 返回所有的模版
/// </summary>
/// <exception cref="ArgumentException"></exception>
/// <returns>模版list</returns>
public List<Model.t_template> list()
{
using (Model.NewDataContext dataContext = new Model.NewDataContext())
{
try
{
return dataContext.t_template.ToList();
}
catch (ArgumentException e)
{

throw e;
}
}
}
}
}

 

posted @ 2010-07-12 10:25  混了好多年  阅读(1136)  评论(0编辑  收藏  举报