Linq自3.5 出世便受到很多人注意,有Linq to sql、Linq to objcet、Linq to Json、Linq to DataSet等等

今日 我也是出于好奇,便对其Linq to sql 进行了感受,感觉与以前使用java的Hibernate或说是Hibernate.net

完全不一样,有点像女子。。。。呵呵

好了 ,开始我们的小小的CRUD操作吧

体系结构: 

BLL:业务逻辑  DAL:数据访问  Model:实体层 Interfaces接口

 数据库表

create table users

{

id int identity(1,1) primary key not null,

userName varchar(50) not null,

pwd varchar(50) not null,

age int not null

}

 

namespace LinqDemo.Model
{
    [Table(Name = "Users")]
    public class UserInfo
    {

        public UserInfo()
        {

        }
        [Column(IsPrimaryKey = true,IsDbGenerated=true)]
        public int id { set; get; }

        [Column]
        public string userName { set; get; }

        [Column]
        public string pwd { set; get; }

        [Column]
        public int age { set; get; }
    }
}

 

namespace LinqDemo.Interface
{
    interface IOperate<T>
    {
            bool Add(T type);
            bool Update(T type);
            bool Delete(int id);
            List<T> GetList();
           
    }
}

 

 

namespace LinqDemo.DAL
{
    public class DALBase
    {
        private DALBase()
        {
        }
        private static DataContext dataContext = null;

        private static string connectString = "server=.;database=demoDB;user=sa;pwd=sa";


        public static DataContext GetDataContext()
        {
            if (dataContext == null)
            {
                dataContext = new DataContext(connectString);
            }
            return dataContext;
        }
    }
}

 

 

using LinqDemo.Interface;
using System.Data.Linq;
using LinqDemo.Model;
/// <summary>
///DALOperate 的摘要说明
/// </summary>
namespace LinqDemo.DAL
{
    public class UserInfoDAL : IOperate<UserInfo>
    {
        public UserInfoDAL()
        {
        }

        public static DataContext DC
        {
            get { return DALBase.GetDataContext(); }

        }
        #region IOperate<UserInfo> 成员

        //用户添加
        public bool Add(UserInfo userInfo)
        {
            try
            {
                DC.GetTable<UserInfo>().InsertOnSubmit(userInfo);
                DC.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //更新用户
        public bool Update(UserInfo userInfo)
        {
            try
            {
                UserInfo user = DC.GetTable<UserInfo>().Single<UserInfo>(u => u.id == userInfo.id);
                user.userName = userInfo.userName;
                user.pwd = userInfo.pwd;
                user.age = userInfo.age;
                DC.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        //根据ID 删除用户
        public bool Delete(int id)
        {
            try
            {
                UserInfo userInfo = DC.GetTable<UserInfo>().Single<UserInfo>(u => u.id == id);
                DC.GetTable<UserInfo>().DeleteOnSubmit(userInfo);
                DC.SubmitChanges();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        //查询用户列表
        public System.Collections.Generic.List<UserInfo> GetList()
        {
            return DC.GetTable<UserInfo>().ToList<UserInfo>();
        }

        #endregion

    }
}

 

namespace LinqDemo.BLL
{
    public class UserInfoBLL
    {
        public UserInfoBLL()
        {
        }

        IOperate<UserInfo> userDAL = new UserInfoDAL();

        public bool AddUser(UserInfo userInfo)
        {
            return userDAL.Add(userInfo);
        }

        public bool Update(UserInfo userInfo)
        {
            return userDAL.Update(userInfo);
        }

        public bool DeleteUser(int id)
        {
            return userDAL.Delete(id);
        }

        public List<UserInfo> GetUserInfoList()
        {
            return userDAL.GetList();
        }
    }
}

 

 

==view层进行模拟操作

 

  protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        SetDataBind();
    }

    UserInfoBLL userBLL = new UserInfoBLL();

    public void SetDataBind()
    {
        GridView1.DataSource = userBLL.GetUserInfoList();

        GridView1.DataBind();
    }

    //添加
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        UserInfo userInfo = new UserInfo { userName = "test", pwd = "456",age=11 };
        userBLL.AddUser(userInfo);
        SetDataBind();
    }

    //修改
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        UserInfo userInfo = new UserInfo { id=1,userName = "我是更新的数据", pwd = "456", age = 11 };
        userBLL.Update(userInfo);
        SetDataBind();
    }

    //删除
    protected void btnDel_Click(object sender, EventArgs e)
    {
        userBLL.DeleteUser(2);
        SetDataBind();
    }

 

  简单的CRUD操作完成   没有一句SQL语句 不错吧

 

 

posted on 2009-06-10 20:21  冷傲残痕  阅读(276)  评论(0编辑  收藏  举报