コード例 #1
0
        private void setAll <TData>(IEnumerable <TData> objects, SqlBulkCopyOptions copyOptions, TableNameResolver nameResolver)
        {
            var connection = GetDefaultConnection();

            var bulkCopy = new SqlBulkCopy(connection, copyOptions, null);

            bulkCopy.SetupBulkCopyByDataType <TData>(nameResolver);

            bool keepClosed = connection.State == ConnectionState.Closed;

            try
            {
                if (connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                }
                using (var dataReader = new ObjectDataReader <TData>(objects))
                {
                    bulkCopy.WriteToServer(dataReader);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("SetAll < " + typeof(TData) + " >", ex);
            }
            finally
            {
                if (keepClosed)
                {
                    connection.Close();
                }
            }
        }