Exemple #1
0
        public static IQueryable <T> StageOnSqlServerUsingBcp <T>(this IEnumerable <T> items, ChoETLSqlServerSettings sqlServerSettings = null)
            where T : class
        {
            sqlServerSettings = ValidateSettings <T>(sqlServerSettings);
            CreateDatabaseIfLocalDb(sqlServerSettings);
            var firstItem = items.FirstOrDefault();

            if (firstItem != null)
            {
                sqlServerSettings.WriteLog($"Opening db: {sqlServerSettings.ConnectionString}");
                using (var conn1 = new SqlConnection(sqlServerSettings.ConnectionString))
                {
                    conn1.Open();
                    try
                    {
                        SqlCommand command = new SqlCommand(firstItem.CreateTableScript(sqlServerSettings.DBColumnDataTypeMapper, sqlServerSettings.TableName), conn1);
                        command.ExecuteNonQuery();
                    }
                    catch { }
                    //Truncate table
                    try
                    {
                        SqlCommand command = new SqlCommand("TRUNCATE TABLE [{0}]".FormatString(sqlServerSettings.TableName), conn1);
                        command.ExecuteNonQuery();
                    }
                    catch
                    {
                        try
                        {
                            SqlCommand command = new SqlCommand("DELETE FROM [{0}]".FormatString(sqlServerSettings.TableName), conn1);
                            command.ExecuteNonQuery();
                        }
                        catch { }
                    }
                }

                using (SqlBulkCopy bcp = new SqlBulkCopy(sqlServerSettings.ConnectionString))
                {
                    bcp.DestinationTableName = sqlServerSettings.TableName;
                    bcp.EnableStreaming      = true;

                    //bcp.NotifyAfter = 10;
                    //bcp.SqlRowsCopied += delegate (object sender, SqlRowsCopiedEventArgs e)
                    //{
                    //    Console.WriteLine(e.RowsCopied.ToString("#,##0") + " rows copied.");
                    //};
                    bcp.WriteToServer(new ChoEnumerableDataReader(items));
                }

                var ctx   = new ChoETLSqlServerDbContext <T>(sqlServerSettings.ConnectionString);
                var dbSet = ctx.Set <T>();
                return(dbSet);
            }
            else
            {
                return(items.AsQueryable());
            }
        }
Exemple #2
0
        public static IQueryable <T> StageOnSqlServer <T>(this IEnumerable <T> items, ChoETLSqlServerSettings sqlServerSettings = null)
            where T : class
        {
            if (typeof(T) == typeof(ExpandoObject) || typeof(T) == typeof(object))
            {
                throw new NotSupportedException();
            }

            Dictionary <string, PropertyInfo> PIDict = ChoType.GetProperties(typeof(T)).ToDictionary(p => p.Name);

            sqlServerSettings = ValidateSettings <T>(sqlServerSettings);
            LoadDataToDb(items, sqlServerSettings, PIDict);

            var ctx   = new ChoETLSqlServerDbContext <T>(sqlServerSettings.ConnectionString);
            var dbSet = ctx.Set <T>();

            return(dbSet);
        }
Exemple #3
0
        public static IQueryable <T> StageOnSqlServer <T>(this IEnumerable <T> items, ChoETLSqlServerSettings sqlServerSettings = null)
            where T : class
        {
            Dictionary <string, PropertyInfo> PIDict = null;

            if (!typeof(T).IsDynamicType() && typeof(T) != typeof(object))
            {
                PIDict = ChoType.GetProperties(typeof(T)).ToDictionary(p => p.Name);
            }

            sqlServerSettings = ValidateSettings <T>(sqlServerSettings);
            LoadDataToDb(items, sqlServerSettings, PIDict);

            var ctx   = new ChoETLSqlServerDbContext <T>(sqlServerSettings.ConnectionString);
            var dbSet = ctx.Set <T>();

            return(dbSet);
        }