![ContractedBlock.gif](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/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方法结束
}
}
// 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方法结束
}
}