Example #1
0
        public void Insert <TEntity>(IEnumerable <TEntity> entities, int batchSize, int commandTimeOut)
        {
            var tableInfo       = MetaDataStore.GetTableInfoFor <TEntity>();
            var insertStatement = tableInfo.GetInsertStatementWithoutReturningTheIdentityValue();

            var sqlCommandSet = new SqlCommandSetWrapper {
                Connection = Connection, Transaction = Transaction
            };

            foreach (var entity in entities)
            {
                var currentCommand = CreateCommand();
                currentCommand.CommandText = insertStatement;

                foreach (var parameterInfo in tableInfo.GetParametersForInsert(entity))
                {
                    currentCommand.CreateAndAddInputParameter(parameterInfo.DbType, "@" + parameterInfo.Name, parameterInfo.Value);
                }

                sqlCommandSet.Append(currentCommand);

                if (sqlCommandSet.CommandCount == batchSize)
                {
                    ExecuteCurrentBatch(sqlCommandSet);
                    sqlCommandSet = new SqlCommandSetWrapper {
                        Connection = Connection, Transaction = Transaction
                    };
                }
            }

            if (sqlCommandSet.CommandCount > 0)
            {
                ExecuteCurrentBatch(sqlCommandSet);
            }
        }
Example #2
0
 private void ExecuteCurrentBatch(SqlCommandSetWrapper sqlCommandSet)
 {
     try
     {
         sqlCommandSet.ExecuteNonQuery();
     }
     finally
     {
         sqlCommandSet.Dispose();
     }
 }