Ejemplo n.º 1
0
        public static IDbConnection GetDbConnection(IDataSourceDb databse)
        {
            IDbConnection connection = null;

            switch (databse.DBTYPE)
            {
            case DataBaseType.SqlServer:
                connection = new SqlConnection();
                break;

            case DataBaseType.MySql:
            case DataBaseType.Sqlite:
                connection = databse.GetNewConnection(false, true);
                break;

            case DataBaseType.Oracle:
                break;

            case DataBaseType.Oledb:
#if NETFRAMEWORK
                connection = new OleDbConnection();
                break;
#else
                throw new NotImplementedException($@".Net Standard haven't implemented this functionality yet.");
#endif

            case DataBaseType.Access95:
#if NETFRAMEWORK
                connection = new OleDbConnection();
                break;
#else
                throw new NotImplementedException($@".Net Standard haven't implemented this functionality yet.");
#endif

            case DataBaseType.Odbc:
#if NETFRAMEWORK
                connection = new OdbcConnection();
                break;
#else
                throw new NotImplementedException($@".Net Standard haven't implemented this functionality yet.");
#endif

            default:
                throw new ArgumentOutOfRangeException();
            }

            if (connection != null)
            {
                connection.ConnectionString = databse.BuildConnectionString();
            }
            return(connection);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Bulks the insert.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="listPoco">The list poco.</param>
        /// <param name="tableName">Name of the table.</param>
        /// <returns>System.Int32.</returns>
        private static int BulkInsertSqlServer <T>(IDataSourceDb database, IEnumerable <T> listPoco, string tableName) where T : class
        {
            using (var bcp = new SqlBulkCopy(database.BuildConnectionString()))
            {
                var openTime = DateTime.Now;
                var bucket   = new QueryBucket()
                {
                    Query        = $"Bulk Insert On List Of Type {typeof(T).Name} Into Table {tableName}",
                    Server       = database.Server,
                    DatabaseType = database.DBTYPE.ToString()
                };
                bcp.SqlRowsCopied += delegate(object sender, SqlRowsCopiedEventArgs args)
                {
                    bucket.ConnectionDisposeTime = DateTime.Now;
                };
                database.QueryBucketManager.AddBucket(bucket);



                var array = new List <string>()
                {
                };
                var advances = ExtFastMember.GetAdvanceMembers(listPoco.FirstOrDefault()).Where(m => m.SqlCustomAttritube.AutoIncrementBy == null && m.SqlCustomAttritube.StartIncrementAt == null && m.SqlCustomAttritube.Ignore != true).ToList(); // EXECLUDES IDENTITY FEILDS
                advances.ForEach(delegate(AdvanceMember member)
                {
                    var columnName = member.SqlCustomAttritube.MapTo ?? member.Member.Name;
                    array.Add(columnName);
                });
                using (var reader = ObjectReader.Create(listPoco, array.ToArray()))
                {
                    bcp.DestinationTableName = tableName;
                    bcp.BulkCopyTimeout      = database.Timeout.TotalSeconds.ToInt(false);
                    bcp.WriteToServer(reader);
                    return(listPoco.Count());
                }
            }
        }