Linq to DB 操作类

ContractedBlock.gif ExpandedBlockStart.gif
 
   
// ==============================================================================
// Created by Bndy at 2010/3/8 9:13:35
// Copyright (c) 2010 Bndy, All rights reserved.
// Welcome to my site http://www.bndy.net
//
// * * * * * * * * * * * * * * * *
// * Q Q : 8 1 7 9 5 7 0 5 *
// * M S N : bndy533@msn.com *
// * Email : bndy533@163.com *
// * * * * * * * * * * * * * * * *
//
// ------------------------------------------------------------------------------
// Linq to DB 操作类
// ==============================================================================

using System;
using System.Linq;
using System.Data.Linq;
using System.Collections.Generic;

namespace Net.Bndy.Data
{
public class Linq2Db
{
static System.Data.Linq.DataContext _dc = null ;

#region 构造函数开始
public Linq2Db(System.Data.IDbConnection connection)
{
_dc
= new System.Data.Linq.DataContext(connection);
}
#endregion 构造函数结束

public System.Collections.Generic.IEnumerable < TEntity > Query < TEntity > (Func < TEntity, bool > predicate) where TEntity : class
{
return GetTable < TEntity > ().Where < TEntity > (predicate).AsEnumerable();
}

public System.Data.Linq.Table < TEntity > GetTable < TEntity > () where TEntity : class
{
return _dc.GetTable < TEntity > ();
}

public void SubmitChanges()
{
try
{
_dc.SubmitChanges();
}
catch
{
foreach (System.Data.Linq.ObjectChangeConflict occ in _dc.ChangeConflicts)
{
occ.Resolve(System.Data.Linq.RefreshMode.KeepCurrentValues);
occ.Resolve(System.Data.Linq.RefreshMode.OverwriteCurrentValues);
occ.Resolve(System.Data.Linq.RefreshMode.KeepChanges);
}
_dc.SubmitChanges();
}

}

#region 封装Table方法开始
public void Attach < TEntity > (TEntity entity) where TEntity : class
{
GetTable
< TEntity > ().Attach(entity);
}

public void Attach < TEntity > (TEntity entity, bool asModified) where TEntity : class
{
GetTable
< TEntity > ().Attach(entity, asModified);
}

public void Attach < TEntity > (TEntity entity, TEntity original) where TEntity : class
{
GetTable
< TEntity > ().Attach(entity, original);
}

public void AttachAll < TSubEntity > (IEnumerable < TSubEntity > entities) where TSubEntity : class
{
GetTable
< TSubEntity > ().AttachAll < TSubEntity > (entities);
}

public void AttachAll < TSubEntity > (IEnumerable < TSubEntity > entities, bool asModified) where TSubEntity : class
{
GetTable
< TSubEntity > ().AttachAll < TSubEntity > (entities, asModified);
}

public void DeleteAllOnSubmit < TSubEntity > (IEnumerable < TSubEntity > entities) where TSubEntity : class
{
GetTable
< TSubEntity > ().DeleteAllOnSubmit < TSubEntity > (entities);
}

public void DeleteOnSubmit < TEntity > (TEntity entity) where TEntity : class
{
GetTable
< TEntity > ().DeleteOnSubmit(entity);
}

public IEnumerator < TEntity > GetEnumerator < TEntity > () where TEntity : class
{
return GetTable < TEntity > ().AsEnumerable < TEntity > ().GetEnumerator();
}

public System.Data.Linq.ModifiedMemberInfo[] GetModifiedMembers < TEntity > (TEntity entity) where TEntity : class
{
return GetTable < TEntity > ().GetModifiedMembers(entity);
}

public System.ComponentModel.IBindingList GetNewBindingList < TEntity > () where TEntity : class
{
return GetTable < TEntity > ().GetNewBindingList();
}

public TEntity GetOriginalEntityState < TEntity > (TEntity entity) where TEntity : class
{
return GetTable < TEntity > ().GetOriginalEntityState(entity);
}

public void InsertAllOnSubmit < TEntity > (IEnumerable < TEntity > entities) where TEntity : class
{
GetTable
< TEntity > ().InsertAllOnSubmit < TEntity > (entities);
}

public void InsertOnSubmit < TEntity > (TEntity entity) where TEntity : class
{
GetTable
< TEntity > ().InsertOnSubmit(entity);
}

public override string ToString()
{
return " Linq2Db Ojbect! " ;
}
#endregion 封装Table方法结束
}

public class Linq2Dt < TEntity > where TEntity : class
{
static System.Data.Linq.DataContext _dc = null ;

#region 构造函数开始
public Linq2Dt(System.Data.IDbConnection connection)
{
_dc
= new System.Data.Linq.DataContext(connection);
}
#endregion 构造函数结束

public System.Collections.Generic.IEnumerable < TEntity > Query(Func < TEntity, bool > predicate)
{
return _dc.GetTable < TEntity > ().AsEnumerable();
}

public System.Data.Linq.Table < TEntity > GetTable()
{
return _dc.GetTable < TEntity > ();
}

public void SubmitChanges()
{
try
{
_dc.SubmitChanges();
}
catch
{
foreach (System.Data.Linq.ObjectChangeConflict occ in _dc.ChangeConflicts)
{
occ.Resolve(System.Data.Linq.RefreshMode.KeepCurrentValues);
occ.Resolve(System.Data.Linq.RefreshMode.OverwriteCurrentValues);
occ.Resolve(System.Data.Linq.RefreshMode.KeepChanges);
}
_dc.SubmitChanges();
}
}

#region 封装Table方法开始
public void Attach(TEntity entity)
{
GetTable().Attach(entity);
}

public void Attach(TEntity entity, bool asModified)
{
GetTable().Attach(entity, asModified);
}

public void Attach(TEntity entity, TEntity original)
{
GetTable().Attach(entity, original);
}

public void AttachAll < TSubEntity > (IEnumerable < TSubEntity > entities) where TSubEntity : TEntity
{
GetTable().AttachAll
< TSubEntity > (entities);
}

public void AttachAll < TSubEntity > (IEnumerable < TSubEntity > entities, bool asModified) where TSubEntity : TEntity
{
GetTable().AttachAll
< TSubEntity > (entities, asModified);
}

public void DeleteAllOnSubmit < TSubEntity > (IEnumerable < TSubEntity > entities) where TSubEntity : TEntity
{
GetTable().DeleteAllOnSubmit
< TSubEntity > (entities);
}

public void DeleteOnSubmit(TEntity entity)
{
GetTable().DeleteOnSubmit(entity);
}

public IEnumerator < TEntity > GetEnumerator()
{
return GetTable().AsEnumerable().GetEnumerator();
}

public System.Data.Linq.ModifiedMemberInfo[] GetModifiedMembers(TEntity entity)
{
return GetTable().GetModifiedMembers(entity);
}

public System.ComponentModel.IBindingList GetNewBindingList()
{
return GetTable().GetNewBindingList();
}

public TEntity GetOriginalEntityState(TEntity entity)
{
return GetTable().GetOriginalEntityState(entity);
}

public void InsertAllOnSubmit(IEnumerable < TEntity > entities)
{
GetTable().InsertAllOnSubmit(entities);
}

public void InsertOnSubmit(TEntity entity)
{
GetTable().InsertOnSubmit(entity);
}

public override string ToString()
{
return " Linq2Dt Ojbect! " ;
}
#endregion 封装Table方法结束
}
}

 

转载于:https://www.cnblogs.com/bndy/articles/1692009.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值