示例#1
0
        public void TestDbBulkInserter()
        {
            //init db
            System.Data.Entity.Database.SetInitializer(new DropCreateDatabaseAlways <InsertContext>());
            AppDomain.CurrentDomain.SetData("DataDirectory", AppDomain.CurrentDomain.BaseDirectory);
            var connectString =
                @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\TestDbBulkInserter.mdf;Initial Catalog=dbinserter;Integrated Security=True;Connect Timeout=30";

            var context = new InsertContext(connectString);

            context.Inits.Add(new Init2());
            context.SaveChanges();

            var inserter = new DbBulkInserter <Entity2>(connectString, "dbo.Seods", BlockContainerOptions.Default,
                                                        GSProduct.SEOD);

            var entities = new Entity2[]
            {
                new Entity2(1, 0.5f, "a"),
                new Entity2(2, 0.6f, "b"),
                new Entity2(3, 0.7f, "c"),
            };

            foreach (var entity in entities)
            {
                inserter.InputBlock.SafePost(entity);
            }
            inserter.InputBlock.Complete();
            inserter.CompletionTask.Wait();

            //test result
            Assert.AreEqual(3, context.Seods.Count());
            Assert.AreEqual("b", context.Seods.FirstOrDefault(s => s.Id == 2).Name);
            Assert.AreEqual(0.7f, context.Seods.FirstOrDefault(s => s.Id == 3).Price);
        }
示例#2
0
        public async Task TestPersonInsertion3()
        {
            string connStr = CreatePeopleTable(3);

            var f = new PeopleFlow(DataflowOptions.Default);

            //will take effect
            TypeAccessorConfig.RegisterMapping <Person, string>(p => p.Name, new DBColumnMapping("PersonTarget", "NameCol", "N/A2", ColumnMappingOption.Overwrite));
            //will take effect
            TypeAccessorConfig.RegisterMapping <Person, int?>(p => p.Age, new DBColumnMapping("PersonTarget", "AgeCol", -2));

            var dbInserter = new DbBulkInserter <Person>(connStr, "dbo.People", DataflowOptions.Default, "PersonTarget");

            f.LinkTo(dbInserter);

            f.Post("{Name: 'aaron', Age: 20}");
            f.Post("{Name: 'bob', Age: 30}");
            f.Post("{Age: 80}");      //Name will be default value: "N/A"
            f.Post("{Name: 'neo' }"); // Age will be default value: -2
            await f.SignalAndWaitForCompletionAsync();

            await dbInserter.CompletionTask;

            using (var conn = LocalDB.GetLocalDB("ExternalMappingTest3"))
            {
                Assert.AreEqual(4, conn.ExecuteScalar <int>("select count(*) from dbo.People"));
                Assert.AreEqual(1, conn.ExecuteScalar <int>("select count(*) from dbo.People where NameCol = 'N/A2'"));
                Assert.AreEqual(0, conn.ExecuteScalar <int>("select count(*) from dbo.People where NameCol = 'N/A'"));
                Assert.AreEqual(1, conn.ExecuteScalar <int>("select count(*) from dbo.People where AgeCol = -2"));
            }
        }
        public async Task TestPersonInsertion2()
        {
            string connStr = CreatePeopleTable(2);
            
            var f = new PeopleFlow(DataflowOptions.Default);

            //will NOT take effect
            TypeAccessorConfig.RegisterMapping<Person, string>(p => p.Name, new DBColumnMapping("PersonTarget", "NameCol", "N/A2"));
            //will take effect
            TypeAccessorConfig.RegisterMapping<Person, int?>(p => p.Age, new DBColumnMapping("PersonTarget", "AgeCol", -2));

            var dbInserter = new DbBulkInserter<Person>(connStr, "dbo.People", DataflowOptions.Default, "PersonTarget");
            f.LinkTo(dbInserter);

            f.Post("{Name: 'aaron', Age: 20}");
            f.Post("{Name: 'bob', Age: 30}");
            f.Post("{Age: 80}"); //Name will be default value: "N/A"
            f.Post("{Name: 'neo' }"); // Age will be default value: -2
            await f.SignalAndWaitForCompletionAsync();
            await dbInserter.CompletionTask;

            using (var conn = LocalDB.GetLocalDB("ExternalMappingTest2"))
            {
                Assert.AreEqual(4, conn.ExecuteScalar<int>("select count(*) from dbo.People"));
                Assert.AreEqual(0, conn.ExecuteScalar<int>("select count(*) from dbo.People where NameCol = 'N/A2'"));
                Assert.AreEqual(1, conn.ExecuteScalar<int>("select count(*) from dbo.People where NameCol = 'N/A'"));
                Assert.AreEqual(1, conn.ExecuteScalar<int>("select count(*) from dbo.People where AgeCol = -2"));
            }
        }
示例#4
0
        public static async Task BulkInserterDemo()
        {
            string connStr;

            //initialize table
            using (var conn = LocalDB.GetLocalDB("BulkInserterDemo"))
            {
                var cmd = new SqlCommand(@"
                IF OBJECT_id('dbo.People', 'U') IS NOT NULL
                    DROP TABLE dbo.People;
                
                CREATE TABLE dbo.People
                (
                    Id INT IDENTITY(1,1) NOT NULL,
                    NameCol nvarchar(50) NOT NULL,
                    AgeCol INT           NOT NULL
                )
                ", conn);
                cmd.ExecuteNonQuery();
                connStr = conn.ConnectionString;
            }

            var f          = new PeopleFlow(DataflowOptions.Default);
            var dbInserter = new DbBulkInserter <Person>(connStr, "dbo.People", DataflowOptions.Default, "PersonTarget");

            f.LinkTo(dbInserter);

            f.Post("{Name: 'aaron', Age: 20}");
            f.Post("{Name: 'bob', Age: 30}");
            f.Post("{Age: 80}");      //Name will be default value: "N/A"
            f.Post("{Name: 'neo' }"); // Age will be default value: -1
            await f.SignalAndWaitForCompletionAsync();

            await dbInserter.CompletionTask;
        }
示例#5
0
        public static async Task BulkInserterDemo2()
        {
            string connStr;

            //initialize table
            using (var conn = LocalDB.GetLocalDB("BulkInserterDemo"))
            {
                var cmd = new SqlCommand(@"
                IF OBJECT_id('dbo.Orders', 'U') IS NOT NULL
                    DROP TABLE dbo.Orders;
                
                CREATE TABLE dbo.Orders
                (
                    Id INT IDENTITY(1,1) NOT NULL,
                    Date DATETIME NOT NULL,
                    Value FLOAT NOT NULL,
                    CustomerName NVARCHAR(50) NOT NULL,
                    CustomerAge INT NOT NULL
                )
                ", conn);
                cmd.ExecuteNonQuery();
                connStr = conn.ConnectionString;
            }

            var dbInserter = new DbBulkInserter <Order>(connStr, "dbo.Orders", DataflowOptions.Default, "OrderTarget");

            dbInserter.Post(new Order {
                OrderDate = DateTime.Now, OrderValue = 15, Customer = new Person()
                {
                    Name = "Aaron", Age = 38
                }
            });
            dbInserter.Post(new Order {
                OrderDate = DateTime.Now, OrderValue = 25, Customer = new Person()
                {
                    Name = "Bob", Age = 30
                }
            });
            dbInserter.Post(new Order {
                OrderDate = DateTime.Now, OrderValue = 35, Customer = new Person()
                {
                    Age = 48
                }
            });
            dbInserter.Post(new Order {
                OrderDate = DateTime.Now, OrderValue = 45, Customer = new Person()
                {
                    Name = "Neo"
                }
            });

            await dbInserter.SignalAndWaitForCompletionAsync();
        }
        public void TestDbBulkInserter_NoNullCheck()
        {
            //init db
            Database.SetInitializer(new DropCreateDatabaseAlways<InsertContext>());
            var connectString = TestUtils.GetLocalDBConnectionString();
            var context = new InsertContext(connectString);
            context.Inits.Add(new Init2());
            context.SaveChanges();

            var inserter = new DbBulkInserter<Entity3>(
                connectString,
                "dbo.Seods",
                DataflowOptions.Default,
                GSProduct.SEOD);

            var guid = Guid.NewGuid();
            var entities = new Entity3[]
                               {
                                   new Entity3(1) { Key = "a", Value = 0.5f, MyLeg = new Leg() {}},
                                   new Entity3(2) { Key = "b", MyLeg = new Leg() { LegInt = 5, LegString = "bleg" } }
                               };

            var entities2 = new[]
                                {
                                    new Entity3(3)
                                        {
                                            Value = 0.7f,
                                            MyLeg = new Leg(),
                                            UID = guid,
                                            RawData = Encoding.ASCII.GetBytes("abc")
                                        }
                                };

            inserter.ProcessMultipleAsync(true, entities, entities2).Wait();

            //test result
            Assert.AreEqual(3, context.Seods.Count());
            Assert.AreEqual("b", context.Seods.Find(2).Name);
            Assert.AreEqual("b", context.Seods.Find(2).Name2);
            Assert.AreEqual(5, context.Seods.Find(2).LegInt);
            Assert.AreEqual("bleg", context.Seods.Find(2).LegString);
            Assert.AreEqual("LegString2", context.Seods.Find(2).LegString2);
            Assert.AreEqual(-2f, context.Seods.First(s => s.Id == 2).Price);
            Assert.AreEqual("default", context.Seods.First(s => s.Id == 3).Name);
            Assert.AreEqual(0.7f, context.Seods.First(s => s.Id == 3).Price);
            Assert.AreEqual(2, context.Seods.Find(3).LegInt);
            Assert.AreEqual("LegString", context.Seods.Find(3).LegString);
            Assert.AreEqual("LegString2", context.Seods.Find(3).LegString2);
            Assert.AreEqual(guid, context.Seods.Find(3).Uid);
            Assert.AreEqual("abc", Encoding.ASCII.GetString(context.Seods.Find(3).RawData));
        }
示例#7
0
        public void TestDbBulkInserter()
        {
            //init db
            Database.SetInitializer(new DropCreateDatabaseAlways <InsertContext>());
            var connectString = TestUtils.GetLocalDBConnectionString();
            var context       = new InsertContext(connectString);

            context.Inits.Add(new Init2());
            context.SaveChanges();

            var inserter = new DbBulkInserter <Entity2>(
                connectString,
                "dbo.Seods",
                DataflowOptions.Default,
                GSProduct.SEOD);

            var entities = new Entity2[]
            {
                new Entity2(1)
                {
                    Key = "a", Value = 0.5f
                },
                new Entity2(2)
                {
                    Key = "b", MyLeg = new Leg()
                    {
                        LegInt = 5, LegString = "bleg"
                    }
                },
                new Entity2(3)
                {
                    Value = 0.7f
                },
            };

            inserter.ProcessAsync(entities, true).Wait();

            //test result
            Assert.AreEqual(3, context.Seods.Count());
            Assert.AreEqual("b", context.Seods.Find(2).Name);
            Assert.AreEqual("b", context.Seods.Find(2).Name2);
            Assert.AreEqual(5, context.Seods.Find(2).LegInt);
            Assert.AreEqual("bleg", context.Seods.Find(2).LegString);
            Assert.AreEqual(-2f, context.Seods.First(s => s.Id == 2).Price);
            Assert.AreEqual("default", context.Seods.First(s => s.Id == 3).Name);
            Assert.AreEqual(0.7f, context.Seods.First(s => s.Id == 3).Price);
            Assert.AreEqual(2, context.Seods.Find(3).LegInt);
            Assert.AreEqual("LegString", context.Seods.Find(3).LegString);
        }
示例#8
0
        public async Task TestDoNoExpand2()
        {
            string connStr = ExternalMappingTest.CreatePeopleTable(4);

            var dbInserter = new DbBulkInserter <PersonEx2>(connStr, "dbo.People", DataflowOptions.Default, "PersonTarget");

            dbInserter.Post(new PersonEx2 {
                Age = 10, Name = "Aaron", Me = new Person {
                    Age = 20, Name = "Bob"
                }
            });
            await dbInserter.SignalAndWaitForCompletionAsync();

            using (var conn = TestUtils.GetLocalDB("ExternalMappingTest4"))
            {
                Assert.AreEqual(1, conn.ExecuteScalar <int>("select count(*) from dbo.People"));
                Assert.AreEqual(1, conn.ExecuteScalar <int>("select count(*) from dbo.People where NameCol = 'Bob'"));
                Assert.AreEqual(1, conn.ExecuteScalar <int>("select count(*) from dbo.People where AgeCol = 20"));
            }
        }
        public void TestDbBulkInserter()
        {
            //init db
            Database.SetInitializer(new DropCreateDatabaseAlways<InsertContext>());
            var connectString = TestUtils.GetLocalDBConnectionString();
            var context = new InsertContext(connectString);
            context.Inits.Add(new Init2());
            context.SaveChanges();
            
            var inserter = new DbBulkInserter<Entity2>(
                connectString,
                "dbo.Seods",
                DataflowOptions.Default,
                GSProduct.SEOD);

            var entities = new Entity2[]
                               {
                                   new Entity2(1) { Key = "a", Value = 0.5f },
                                   new Entity2(2) { Key = "b", MyLeg = new Leg() { LegInt = 5, LegString = "bleg" } },
                                   new Entity2(3) { Value = 0.7f },
                               };

            inserter.ProcessAsync(entities, true).Wait();

            //test result
            Assert.AreEqual(3, context.Seods.Count());
            Assert.AreEqual("b", context.Seods.Find(2).Name);
            Assert.AreEqual("b", context.Seods.Find(2).Name2);
            Assert.AreEqual(5, context.Seods.Find(2).LegInt);
            Assert.AreEqual("bleg", context.Seods.Find(2).LegString);
            Assert.AreEqual(-2f, context.Seods.First(s => s.Id == 2).Price);
            Assert.AreEqual("default", context.Seods.First(s => s.Id == 3).Name);
            Assert.AreEqual(0.7f, context.Seods.First(s => s.Id == 3).Price);
            Assert.AreEqual(2, context.Seods.Find(3).LegInt);
            Assert.AreEqual("LegString", context.Seods.Find(3).LegString);

        }
示例#10
0
        public async Task TestOrderInsertion()
        {
            string connStr;

            //initialize table
            using (var conn = LocalDB.GetLocalDB("ExternalMappingTest"))
            {
                var cmd = new SqlCommand(@"
                IF OBJECT_id('dbo.Orders', 'U') IS NOT NULL
                    DROP TABLE dbo.Orders;
                
                CREATE TABLE dbo.Orders
                (
                    Id INT IDENTITY(1,1) NOT NULL,
                    Date DATETIME NOT NULL,
                    Value FLOAT NOT NULL,
                    CustomerName NVARCHAR(50) NOT NULL,
                    CustomerAge INT NOT NULL
                )
                ", conn);
                cmd.ExecuteNonQuery();
                connStr = conn.ConnectionString;
            }

            //will take effect
            TypeAccessorConfig.RegisterMapping <Order, string>(p => p.Customer.Name, new DBColumnMapping("OrderTarget", "CustomerName", "N/A2"));
            //will take effect
            TypeAccessorConfig.RegisterMapping <Order, int?>(p => p.Customer.Age, new DBColumnMapping("OrderTarget", "CustomerAge", -99));

            var dbInserter = new DbBulkInserter <Order>(connStr, "dbo.Orders", DataflowOptions.Default, "OrderTarget");

            dbInserter.Post(new Order {
                OrderDate = DateTime.Now, OrderValue = 15, Customer = new Person()
                {
                    Name = "Aaron", Age = 38
                }
            });
            dbInserter.Post(new Order {
                OrderDate = DateTime.Now, OrderValue = 25, Customer = new Person()
                {
                    Name = "Bob", Age = 30
                }
            });
            dbInserter.Post(new Order {
                OrderDate = DateTime.Now, OrderValue = 35, Customer = new Person()
                {
                    Age = 48
                }
            });
            dbInserter.Post(new Order {
                OrderDate = DateTime.Now, OrderValue = 45, Customer = new Person()
                {
                    Name = "Neo"
                }
            });

            await dbInserter.SignalAndWaitForCompletionAsync();

            using (var conn = LocalDB.GetLocalDB("ExternalMappingTest"))
            {
                Assert.AreEqual(4, conn.ExecuteScalar <int>("select count(*) from dbo.Orders"));
                Assert.AreEqual(1, conn.ExecuteScalar <int>("select count(*) from dbo.Orders where CustomerAge = 48 and CustomerName = 'N/A2'"));
                Assert.AreEqual(0, conn.ExecuteScalar <int>("select count(*) from dbo.Orders where CustomerName = 'N/A'"));
                Assert.AreEqual(1, conn.ExecuteScalar <int>("select count(*) from dbo.Orders where CustomerName = 'Neo' and CustomerAge = -99"));
            }
        }
示例#11
0
        public void TestDbBulkInserter_NoNullCheck()
        {
            //init db
            var connectString = TestUtils.GetLocalDBConnectionString();
            var context       = new InsertContext(connectString);

            context.Inits.Add(new Init2());
            context.SaveChanges();

            var inserter = new DbBulkInserter <Entity3>(
                connectString,
                "dbo.Seods",
                DataflowOptions.Default,
                GSProduct.SEOD);

            var guid     = Guid.NewGuid();
            var entities = new Entity3[]
            {
                new Entity3(1)
                {
                    Key = "a", Value = 0.5f, MyLeg = new Leg()
                    {
                    }
                },
                new Entity3(2)
                {
                    Key = "b", MyLeg = new Leg()
                    {
                        LegInt = 5, LegString = "bleg"
                    }
                }
            };

            var entities2 = new[]
            {
                new Entity3(3)
                {
                    Value   = 0.7f,
                    MyLeg   = new Leg(),
                    UID     = guid,
                    RawData = Encoding.ASCII.GetBytes("abc")
                }
            };

            inserter.ProcessMultipleAsync(true, entities, entities2).Wait();

            //test result
            Assert.AreEqual(3, context.Seods.Count());
            Assert.AreEqual("b", context.Seods.Find(2).Name);
            Assert.AreEqual("b", context.Seods.Find(2).Name2);
            Assert.AreEqual(5, context.Seods.Find(2).LegInt);
            Assert.AreEqual("bleg", context.Seods.Find(2).LegString);
            Assert.AreEqual("LegString2", context.Seods.Find(2).LegString2);
            Assert.AreEqual(-2f, context.Seods.First(s => s.Id == 2).Price);
            Assert.AreEqual("default", context.Seods.First(s => s.Id == 3).Name);
            Assert.AreEqual(0.7f, context.Seods.First(s => s.Id == 3).Price);
            Assert.AreEqual(2, context.Seods.Find(3).LegInt);
            Assert.AreEqual("LegString", context.Seods.Find(3).LegString);
            Assert.AreEqual("LegString2", context.Seods.Find(3).LegString2);
            Assert.AreEqual(guid, context.Seods.Find(3).Uid);
            Assert.AreEqual("abc", Encoding.ASCII.GetString(context.Seods.Find(3).RawData));
        }
示例#12
0
        public static async Task ETLLookupDemo()
        {
            string connStr;

            //initialize table
            using (var conn = LocalDB.GetLocalDB("ETLLookupDemo"))
            {
                //We will create a dimension table and a fact table
                //We also populate the dimension table with some pre-defined rows
                var cmd = new SqlCommand(@"
                IF OBJECT_id('dbo.Product', 'U') IS NOT NULL
                    DROP TABLE dbo.Product;
                
                CREATE TABLE dbo.Product
                (
                    ProductKey INT IDENTITY(1,1) NOT NULL,
                    Category nvarchar(50) NOT NULL,
                    ProductName nvarchar(50) NOT NULL,
                    ProductFullName nvarchar(100) NOT NULL                    
                )

                INSERT INTO dbo.Product VALUES ('Books', 'The Great Gatsby', 'Books-The Great Gatsby');
                INSERT INTO dbo.Product VALUES ('Games', 'Call of Duty', 'Games-Call of Duty');

                IF OBJECT_id('dbo.FactOrders', 'U') IS NOT NULL
                    DROP TABLE dbo.FactOrders;
                
                CREATE TABLE dbo.FactOrders
                (
                    Id INT IDENTITY(1,1) NOT NULL,
                    Date DATETIME NOT NULL,
                    Value FLOAT NOT NULL,
                    ProductKey INT NULL,
                )
                ", conn);
                cmd.ExecuteNonQuery();
                connStr = conn.ConnectionString;
            }

            var lookupNode = new ProductLookupFlow(new TargetTable("LookupDemo", connStr, "dbo.Product"));

            lookupNode.Post(new OrderEx {
                OrderValue = 10, Product = new Product("Books", "The Call of the Wild")
            });
            lookupNode.Post(new OrderEx {
                OrderValue = 20, Product = new Product("Games", "Call of Duty")
            });
            lookupNode.Post(new OrderEx {
                OrderValue = 30, Product = new Product("Games", "Call of Duty")
            });
            lookupNode.Post(new OrderEx {
                OrderValue = 20, Product = new Product("Books", "The Call of the Wild")
            });
            lookupNode.Post(new OrderEx {
                OrderValue = 20, Product = new Product("Books", "The Great Gatsby")
            });

            var factInserter = new DbBulkInserter <OrderEx>(connStr, "dbo.FactOrders", DataflowOptions.Default, "OrderTarget");

            lookupNode.LinkTo(factInserter);

            await lookupNode.SignalAndWaitForCompletionAsync();

            await factInserter.CompletionTask;
        }
        public async Task TestOrderInsertion()
        {
            string connStr;

            //initialize table
            using (var conn = LocalDB.GetLocalDB("ExternalMappingTest"))
            {
                var cmd = new SqlCommand(@"
                IF OBJECT_id('dbo.Orders', 'U') IS NOT NULL
                    DROP TABLE dbo.Orders;
                
                CREATE TABLE dbo.Orders
                (
                    Id INT IDENTITY(1,1) NOT NULL,
                    Date DATETIME NOT NULL,
                    Value FLOAT NOT NULL,
                    CustomerName NVARCHAR(50) NOT NULL,
                    CustomerAge INT NOT NULL
                )
                ", conn);
                cmd.ExecuteNonQuery();
                connStr = conn.ConnectionString;
            }

            //will take effect
            TypeAccessorConfig.RegisterMapping<Order, string>(p => p.Customer.Name, new DBColumnMapping("OrderTarget", "CustomerName", "N/A2"));
            //will take effect
            TypeAccessorConfig.RegisterMapping<Order, int?>(p => p.Customer.Age, new DBColumnMapping("OrderTarget", "CustomerAge", -99));

            var dbInserter = new DbBulkInserter<Order>(connStr, "dbo.Orders", DataflowOptions.Default, "OrderTarget", bulkSize: 3);

            dbInserter.Post(new Order { OrderDate = DateTime.Now, OrderValue = 15, Customer = new Person() { Name = "Aaron", Age = 38 } });
            dbInserter.Post(new Order { OrderDate = DateTime.Now, OrderValue = 25, Customer = new Person() { Name = "Bob", Age = 30 } });
            dbInserter.Post(new Order { OrderDate = DateTime.Now, OrderValue = 35, Customer = new Person() { Age = 48 } });
            dbInserter.Post(new Order { OrderDate = DateTime.Now, OrderValue = 45, Customer = new Person() { Name = "Neo" } });

            await dbInserter.SignalAndWaitForCompletionAsync();

            using (var conn = LocalDB.GetLocalDB("ExternalMappingTest"))
            {
                Assert.AreEqual(4, conn.ExecuteScalar<int>("select count(*) from dbo.Orders"));
                Assert.AreEqual(1, conn.ExecuteScalar<int>("select count(*) from dbo.Orders where CustomerAge = 48 and CustomerName = 'N/A2'"));
                Assert.AreEqual(0, conn.ExecuteScalar<int>("select count(*) from dbo.Orders where CustomerName = 'N/A'"));
                Assert.AreEqual(1, conn.ExecuteScalar<int>("select count(*) from dbo.Orders where CustomerName = 'Neo' and CustomerAge = -99"));
            }
        }
示例#14
0
        public static async Task ETLLookupDemo()
        {
            string connStr;

            //initialize table
            using (var conn = LocalDB.GetLocalDB("ETLLookupDemo"))
            {
                //We will create a dimension table and a fact table
                //We also populate the dimension table with some pre-defined rows
                var cmd = new SqlCommand(@"
                IF OBJECT_id('dbo.Product', 'U') IS NOT NULL
                    DROP TABLE dbo.Product;
                
                CREATE TABLE dbo.Product
                (
                    ProductKey INT IDENTITY(1,1) NOT NULL,
                    Category nvarchar(50) NOT NULL,
                    ProductName nvarchar(50) NOT NULL,
                    ProductFullName nvarchar(100) NOT NULL                    
                )

                INSERT INTO dbo.Product VALUES ('Books', 'The Great Gatsby', 'Books-The Great Gatsby');
                INSERT INTO dbo.Product VALUES ('Games', 'Call of Duty', 'Games-Call of Duty');

                IF OBJECT_id('dbo.FactOrders', 'U') IS NOT NULL
                    DROP TABLE dbo.FactOrders;
                
                CREATE TABLE dbo.FactOrders
                (
                    Id INT IDENTITY(1,1) NOT NULL,
                    Date DATETIME NOT NULL,
                    Value FLOAT NOT NULL,
                    ProductKey INT NULL,
                )
                ", conn);
                cmd.ExecuteNonQuery();
                connStr = conn.ConnectionString;
            }

            var lookupNode = new ProductLookupFlow(new TargetTable("LookupDemo", connStr, "dbo.Product"));

            lookupNode.Post(new OrderEx { OrderValue = 10, Product = new Product("Books", "The Call of the Wild") });
            lookupNode.Post(new OrderEx { OrderValue = 20, Product = new Product("Games", "Call of Duty") });
            lookupNode.Post(new OrderEx { OrderValue = 30, Product = new Product("Games", "Call of Duty") });
            lookupNode.Post(new OrderEx { OrderValue = 20, Product = new Product("Books", "The Call of the Wild") });
            lookupNode.Post(new OrderEx { OrderValue = 20, Product = new Product("Books", "The Great Gatsby") });

            var factInserter = new DbBulkInserter<OrderEx>(connStr, "dbo.FactOrders", DataflowOptions.Default, "OrderTarget");
            lookupNode.LinkTo(factInserter);

            await lookupNode.SignalAndWaitForCompletionAsync();
            await factInserter.CompletionTask;
        }
示例#15
0
        public static async Task BulkInserterDemo2()
        {
            string connStr;

            //initialize table
            using (var conn = LocalDB.GetLocalDB("BulkInserterDemo"))
            {
                var cmd = new SqlCommand(@"
                IF OBJECT_id('dbo.Orders', 'U') IS NOT NULL
                    DROP TABLE dbo.Orders;
                
                CREATE TABLE dbo.Orders
                (
                    Id INT IDENTITY(1,1) NOT NULL,
                    Date DATETIME NOT NULL,
                    Value FLOAT NOT NULL,
                    CustomerName NVARCHAR(50) NOT NULL,
                    CustomerAge INT NOT NULL
                )
                ", conn);
                cmd.ExecuteNonQuery();
                connStr = conn.ConnectionString;
            }

            var dbInserter = new DbBulkInserter<Order>(connStr, "dbo.Orders", DataflowOptions.Default, "OrderTarget");

            dbInserter.Post(new Order {OrderDate = DateTime.Now, OrderValue = 15, Customer = new Person() {Name = "Aaron", Age = 38}}); 
            dbInserter.Post(new Order {OrderDate = DateTime.Now, OrderValue = 25, Customer = new Person() {Name = "Bob", Age = 30}}); 
            dbInserter.Post(new Order {OrderDate = DateTime.Now, OrderValue = 35, Customer = new Person() {Age = 48}}); 
            dbInserter.Post(new Order {OrderDate = DateTime.Now, OrderValue = 45, Customer = new Person() {Name = "Neo"}});

            await dbInserter.SignalAndWaitForCompletionAsync();
        }
示例#16
0
        public static async Task BulkInserterDemo()
        {
            string connStr;

            //initialize table
            using (var conn = LocalDB.GetLocalDB("BulkInserterDemo"))
            {
                var cmd = new SqlCommand(@"
                IF OBJECT_id('dbo.People', 'U') IS NOT NULL
                    DROP TABLE dbo.People;
                
                CREATE TABLE dbo.People
                (
                    Id INT IDENTITY(1,1) NOT NULL,
                    NameCol nvarchar(50) NOT NULL,
                    AgeCol INT           NOT NULL
                )
                ", conn);
                cmd.ExecuteNonQuery();
                connStr = conn.ConnectionString;
            }

            var f = new PeopleFlow(DataflowOptions.Default);
            var dbInserter = new DbBulkInserter<Person>(connStr, "dbo.People", DataflowOptions.Default, "PersonTarget");
            f.LinkTo(dbInserter);

            f.Post("{Name: 'aaron', Age: 20}");
            f.Post("{Name: 'bob', Age: 30}");
            f.Post("{Age: 80}"); //Name will be default value: "N/A"
            f.Post("{Name: 'neo' }"); // Age will be default value: -1
            await f.SignalAndWaitForCompletionAsync();
            await dbInserter.CompletionTask;
        }