Created
September 21, 2017 00:14
-
-
Save rqx110/e6c84925861c03be6159fae2720fef27 to your computer and use it in GitHub Desktop.
abp execute sql command/query and bulkinsert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Abp.Domain.Entities; | |
namespace YnTxOA.SqlExecuterInterface | |
{ | |
public interface ISqlExecuter | |
{ | |
/// <summary> | |
/// 执行给定的命令 | |
/// </summary> | |
/// <param name="sql">命令字符串</param> | |
/// <param name="parameters">要应用于命令字符串的参数</param> | |
/// <returns>执行命令后由数据库返回的结果</returns> | |
int Execute(string sql, params object[] parameters); | |
/// <summary> | |
/// 创建一个原始 SQL 查询,该查询将返回给定泛型类型的元素。 | |
/// </summary> | |
/// <typeparam name="T">查询所返回对象的类型</typeparam> | |
/// <param name="sql">SQL 查询字符串</param> | |
/// <param name="parameters">要应用于 SQL 查询字符串的参数</param> | |
/// <returns></returns> | |
IQueryable<T> SqlQuery<T>(string sql, params object[] parameters); | |
int BulkInsert<TEntity>(IEnumerable<TEntity> ntities) where TEntity : class; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Runtime.Remoting.Metadata.W3cXsd2001; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Abp.Dependency; | |
using Abp.Domain.Entities; | |
using Abp.EntityFramework; | |
using YnTxOA.SqlExecuterInterface; | |
using EntityFramework.BulkExtensions; | |
namespace YnTxOA.EntityFramework.Repositories | |
{ | |
public class SqlExecuter : ISqlExecuter, ITransientDependency | |
{ | |
private readonly IDbContextProvider<AbpZeroTemplateDbContext> _dbContextProvider; | |
public SqlExecuter(IDbContextProvider<AbpZeroTemplateDbContext> dbContextProvider) | |
{ | |
_dbContextProvider = dbContextProvider; | |
} | |
/// <summary> | |
/// 执行给定的命令 | |
/// </summary> | |
/// <param name="sql">命令字符串</param> | |
/// <param name="parameters">要应用于命令字符串的参数</param> | |
/// <returns>执行命令后由数据库返回的结果</returns> | |
public int Execute(string sql, params object[] parameters) | |
{ | |
return _dbContextProvider.GetDbContext().Database.ExecuteSqlCommand(sql, parameters); | |
} | |
/// <summary> | |
/// 创建一个原始 SQL 查询,该查询将返回给定泛型类型的元素。 | |
/// </summary> | |
/// <typeparam name="T">查询所返回对象的类型</typeparam> | |
/// <param name="sql">SQL 查询字符串</param> | |
/// <param name="parameters">要应用于 SQL 查询字符串的参数</param> | |
/// <returns></returns> | |
public IQueryable<T> SqlQuery<T>(string sql, params object[] parameters) | |
{ | |
return _dbContextProvider.GetDbContext().Database.SqlQuery<T>(sql, parameters).AsQueryable(); | |
} | |
public int BulkInsert<TEntity>(IEnumerable<TEntity> ntities) where TEntity : class | |
{ | |
return _dbContextProvider.GetDbContext().BulkInsert<TEntity>(ntities, InsertOptions.Default); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment