123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- using Ant.Core;
- using Ant.Core.Visitors;
- using Ant.Data;
- using Ant.DbExpressions;
- using Ant.Descriptors;
- using Ant.Entity;
- using Ant.Exceptions;
- using Ant.Infrastructure;
- using Ant.ORM;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using System.Text;
- using System.Transactions;
- namespace Ant.SqlServer
- {
- /// <summary>
- ///
- /// </summary>
- public class MsSqlContext : DbContext
- {
- DbContextServiceProvider _dbContextServiceProvider;
- public MsSqlContext()
- {
- this.PagingMode = PagingMode.ROW_NUMBER;
- this._dbContextServiceProvider = new DbContextServiceProvider(PagingMode);
- }
- /// <summary>
- /// 数据库配置
- /// </summary>
- public string ConnectionString { get; set; }
- /// <summary>
- /// 数据库类型
- /// </summary>
- public override DatabaseType DatabaseType
- {
- get { return DatabaseType.MSSQLServer; }
- }
- /// <summary>
- /// 数据库配置
- /// </summary>
- public override DataAccess db
- {
- get { return new MSSqlDataAccess(ConnectionString); }
- }
- /// <summary>
- /// 开启事务
- /// </summary>
- public void BeginTransaction()
- {
- db.BeginTransaction();
- }
- /// <summary>
- /// 提交事务
- /// </summary>
- public void Commit()
- {
- db.Commit();
- }
- /// <summary>
- /// 回滚事务
- /// </summary>
- public void RollBack()
- {
- db.RollBack();
- }
- /// <summary>
- /// 调用说明
- /// public bool DataAcessPackageTrans(out string error)
- //{
- // var db = DBExtend;
- // //简化了事务写法,自动提交回滚
- // return DataAcessPackageTrans(db,(out string ex) =>
- // {
- // ex = "";
- // var product = new ProductData();
- // product.BarCode = "code" + DateTime.Now.Millisecond;
- // product.Number = 10;
- // db.InsertFromObj(product);
- // return false; //会回滚
- // }, out error);
- //}
- /// 使用DbTransaction封装事务(不推荐)
- /// </summary>
- /// <param name="db"></param>
- /// <param name="method"></param>
- /// <param name="error"></param>
- /// <returns></returns>
- public bool DataAcessPackageTrans(DataAccess sqldb, TransMethod method, out string error)
- {
- error = "";
- sqldb.BeginTransaction();
- try
- {
- var a = method(out error);
- if (!a)
- {
- sqldb.RollBack();
- return false;
- }
- sqldb.Commit();
- }
- catch (Exception ero)
- {
- error = "提交事务时发生错误:" + ero.Message;
- sqldb.RollBack();
- return false;
- }
- return true;
- }
- /// <summary>
- /// 调用说明
- /// public bool TransactionTest(out string error)
- //{
- // //简化了事务写法,自动提交回滚
- // return PackageTrans((out string ex) =>
- // {
- // ex = "";
- // var product = new ProductData();
- // product.BarCode = "code" + DateTime.Now.Millisecond;
- // product.Number = 10;
- // ProductDataManage.Instance.Add(product);
- // return false; //会回滚
- // }, out error);
- //}
- /// 使用TransactionScope封装事务[基本方法]分布式事务
- /// </summary>
- /// <param name="method"></param>
- /// <param name="error"></param>
- /// <returns></returns>
- public bool PackageTrans(TransMethod method, out string error)
- {
- error = "";
- using (var trans = new TransactionScope())
- {
- try
- {
- var a = method(out error);
- if (!a)
- {
- return false;
- }
- trans.Complete();
- }
- catch (Exception ero)
- {
- error = "提交事务时发生错误:" + ero.Message;
- return false;
- }
- }
- return true;
- }
- /// <summary>
- /// 数据库分页类型(SQL2005或者2008下,使用ROW_NUMBER()分页)(SQL2012下,使用OFFSET/FETCH NEXT分页)
- /// </summary>
- public PagingMode PagingMode { get; set; }
- /// <summary>
- /// 创建用哪种数据库解析SQL语句
- /// </summary>
- public override IDbContextServiceProvider DbContextServiceProvider
- {
- get { return this._dbContextServiceProvider; }
- }
- }
- }
|