コード例 #1
0
        private static BulkInsertResult <T> BulkInsert <T>(IEnumerable <T> entities, BulkOptions options, TableMapping tableMapping, SqlConnection dbConnection, SqlTransaction transaction, string tableName,
                                                           string[] inputColumns = null, SqlBulkCopyOptions bulkCopyOptions = SqlBulkCopyOptions.Default, bool useInteralId = false)
        {
            var dataReader = new EntityDataReader <T>(tableMapping, entities, useInteralId);

            var sqlBulkCopy = new SqlBulkCopy(dbConnection, bulkCopyOptions, transaction)
            {
                DestinationTableName = tableName,
                BatchSize            = options.BatchSize
            };

            foreach (var column in dataReader.TableMapping.Columns)
            {
                if (inputColumns == null || (inputColumns != null && inputColumns.Contains(column.Column.Name)))
                {
                    sqlBulkCopy.ColumnMappings.Add(column.Property.Name, column.Column.Name);
                }
            }
            if (useInteralId)
            {
                sqlBulkCopy.ColumnMappings.Add(Constants.Guid_ColumnName, Constants.Guid_ColumnName);
            }
            sqlBulkCopy.WriteToServer(dataReader);

            return(new BulkInsertResult <T> {
                RowsAffected = Convert.ToInt32(sqlBulkCopy.GetPrivateFieldValue("_rowsCopied")),
                EntityMap = dataReader.EntityMap
            });
        }
コード例 #2
0
        internal static int ExecuteSql(string query, SqlConnection connection, SqlTransaction transaction, BulkOptions options)
        {
            var sqlCommand = new SqlCommand(query, connection, transaction);

            if (options?.CommandTimeout.HasValue ?? false)
            {
                sqlCommand.CommandTimeout = options.CommandTimeout.Value;
            }
            return(sqlCommand.ExecuteNonQuery());
        }
        private static BulkQueryResult BulkQuery(this DbContext context, string sqlText, SqlConnection dbConnection, SqlTransaction transaction, BulkOptions options)
        {
            var results = new List <object[]>();
            var columns = new List <string>();
            var command = new SqlCommand(sqlText, dbConnection, transaction);

            if (options.CommandTimeout.HasValue)
            {
                command.CommandTimeout = options.CommandTimeout.Value;
            }
            var reader = command.ExecuteReader();

            //Get column names
            for (int i = 0; i < reader.FieldCount; i++)
            {
                columns.Add(reader.GetName(i));
            }
            //Read data
            while (reader.Read())
            {
                Object[] values = new Object[reader.FieldCount];
                reader.GetValues(values);
                results.Add(values);
            }
            return(new BulkQueryResult
            {
                Columns = columns,
                Results = results,
                RowsAffected = reader.RecordsAffected
            });
        }