/// <summary> /// Bulk insert method for bulking a IEnumerable of entities. /// </summary> /// <typeparam name="T">Type of entity</typeparam> /// <param name="dbConnection"></param> /// <param name="entities"></param> /// <param name="bulkOptions">Optional bulk options.</param> public static void BulkInsert <T>(this IDbConnection dbConnection, IEnumerable <T> entities, BulkOptions bulkOptions = null) where T : class { if (dbConnection == null || entities == null) { throw new ArgumentNullException("Invalid null arguments"); } Contract.EndContractBlock(); if (bulkOptions == null) { bulkOptions = new BulkOptions(); } if (dbConnection.State != ConnectionState.Open) { dbConnection.Open(); } DataTable dataTable = BuildDataTableForEntities(entities); CallDatabaseSpecificOperation(dbConnection, BulkOperationsEnum.BulkInsert, dataTable, bulkOptions); }
private static void CallDatabaseSpecificOperation(IDbConnection dbConnection, BulkOperationsEnum bulkOperationsEnum, DataTable dataTable, BulkOptions bulkOptions) { IDatabaseSpecificImplementation implementation = GetDatabaseSpecificImplementation(dbConnection); switch (bulkOperationsEnum) { case BulkOperationsEnum.BulkInsert: implementation.BulkInsert(dbConnection, dataTable, bulkOptions); return; default: throw new Exception("Unknown database."); } }