GetOpenConnection() public static method

public static GetOpenConnection ( ) : SqlConnection
return System.Data.SqlClient.SqlConnection
Ejemplo n.º 1
0
        public void TestMultiMapArbitraryWithSplitAsync()
        {
            var sql = @"select 1 as id, 'abc' as name, 2 as id, 'def' as name";

            using (var connection = Program.GetOpenConnection()) {
                var productQuery = connection.QueryAsync <Product>(sql, new[] { typeof(Product), typeof(Category) }, (objects) => {
                    var prod      = (Product)objects[0];
                    prod.Category = (Category)objects[1];
                    return(prod);
                });

                var product = productQuery.Result.First();
                // assertions
                product.Id.IsEqualTo(1);
                product.Name.IsEqualTo("abc");
                product.Category.Id.IsEqualTo(2);
                product.Category.Name.IsEqualTo("def");
            }
        }
Ejemplo n.º 2
0
        public void RunSequentialVersusParallelSync()
        {
            var ids = Enumerable.Range(1, 20000).Select(id => new { id }).ToArray();

            using (var connection = Program.GetOpenConnection(true))
            {
                connection.Execute(new CommandDefinition("select @id", ids.Take(5), flags: CommandFlags.None));

                var watch = Stopwatch.StartNew();
                connection.Execute(new CommandDefinition("select @id", ids, flags: CommandFlags.None));
                watch.Stop();
                System.Console.WriteLine("No pipeline: {0}ms", watch.ElapsedMilliseconds);

                watch = Stopwatch.StartNew();
                connection.Execute(new CommandDefinition("select @id", ids, flags: CommandFlags.Pipelined));
                watch.Stop();
                System.Console.WriteLine("Pipeline: {0}ms", watch.ElapsedMilliseconds);
            }
        }
Ejemplo n.º 3
0
        public void Issue22_ExecuteScalar()
        {
            using (var connection = Program.GetOpenConnection())
            {
                int i = connection.ExecuteScalarAsync <int>("select 123").Result;
                i.IsEqualTo(123);

                i = connection.ExecuteScalarAsync <int>("select cast(123 as bigint)").Result;
                i.IsEqualTo(123);

                long j = connection.ExecuteScalarAsync <long>("select 123").Result;
                j.IsEqualTo(123L);

                j = connection.ExecuteScalarAsync <long>("select cast(123 as bigint)").Result;
                j.IsEqualTo(123L);

                int?k = connection.ExecuteScalar <int?>("select @i", new { i = default(int?) });
                k.IsNull();
            }
        }
Ejemplo n.º 4
0
        public void TestMultiMapWithSplitAsync()
        {
            var sql = @"select 1 as id, 'abc' as name, 2 as id, 'def' as name";

            using (var connection = Program.GetOpenConnection())
            {
                var productQuery = connection.QueryAsync <Product, Category, Product>(sql, (prod, cat) =>
                {
                    prod.Category = cat;
                    return(prod);
                });

                var product = productQuery.Result.First();
                // assertions
                product.Id.IsEqualTo(1);
                product.Name.IsEqualTo("abc");
                product.Category.Id.IsEqualTo(2);
                product.Category.Name.IsEqualTo("def");
            }
        }
Ejemplo n.º 5
0
        public void TestSupportForDynamicParametersOutputExpressions_QueryMultiple()
        {
            using (var connection = Program.GetOpenConnection())
            {
                var bob = new Person {
                    Name = "bob", PersonId = 1, Address = new Address {
                        PersonId = 2
                    }
                };

                var p = new DynamicParameters(bob);
                p.Output(bob, b => b.PersonId);
                p.Output(bob, b => b.Occupation);
                p.Output(bob, b => b.NumberOfLegs);
                p.Output(bob, b => b.Address.Name);
                p.Output(bob, b => b.Address.PersonId);

                int x, y;
                using (var multi = connection.QueryMultipleAsync(@"
SET @Occupation = 'grillmaster' 
SET @PersonId = @PersonId + 1 
SET @NumberOfLegs = @NumberOfLegs - 1
SET @AddressName = 'bobs burgers'
select 42
select 17
SET @AddressPersonId = @PersonId", p).Result)
                {
                    x = multi.ReadAsync <int>().Result.Single();
                    y = multi.ReadAsync <int>().Result.Single();
                }

                bob.Occupation.IsEqualTo("grillmaster");
                bob.PersonId.IsEqualTo(2);
                bob.NumberOfLegs.IsEqualTo(1);
                bob.Address.Name.IsEqualTo("bobs burgers");
                bob.Address.PersonId.IsEqualTo(2);
                x.IsEqualTo(42);
                y.IsEqualTo(17);
            }
        }
Ejemplo n.º 6
0
        public void TestSubsequentQueriesSuccess()
        {
            using (var connection = Program.GetOpenConnection())
            {
                var data0 = connection.QueryAsync <Foo0>("select 1 as [Id] where 1 = 0").Result.ToList();
                data0.Count().IsEqualTo(0);

                var data1 = connection.QueryAsync <Foo1>(new CommandDefinition("select 1 as [Id] where 1 = 0", flags: CommandFlags.Buffered)).Result.ToList();
                data1.Count().IsEqualTo(0);

                var data2 = connection.QueryAsync <Foo2>(new CommandDefinition("select 1 as [Id] where 1 = 0", flags: CommandFlags.None)).Result.ToList();
                data2.Count().IsEqualTo(0);

                data0 = connection.QueryAsync <Foo0>("select 1 as [Id] where 1 = 0").Result.ToList();
                data0.Count().IsEqualTo(0);

                data1 = connection.QueryAsync <Foo1>(new CommandDefinition("select 1 as [Id] where 1 = 0", flags: CommandFlags.Buffered)).Result.ToList();
                data1.Count().IsEqualTo(0);

                data2 = connection.QueryAsync <Foo2>(new CommandDefinition("select 1 as [Id] where 1 = 0", flags: CommandFlags.None)).Result.ToList();
                data2.Count().IsEqualTo(0);
            }
        }
Ejemplo n.º 7
0
 public void LiteralReplacementDynamicOpen()
 {
     using (var conn = Program.GetOpenConnection()) LiteralReplacementDynamic(conn);
 }
Ejemplo n.º 8
0
        public void TestMultiMapArbitraryMaps()
        {
            // please excuse the trite example, but it is easier to follow than a more real-world one
            var createSql = @"
                create table #ReviewBoards (Id int, Name varchar(20), User1Id int, User2Id int, User3Id int, User4Id int, User5Id int, User6Id int, User7Id int, User8Id int, User9Id int)
                create table #Users (Id int, Name varchar(20))

                insert #Users values(1, 'User 1')
                insert #Users values(2, 'User 2')
                insert #Users values(3, 'User 3')
                insert #Users values(4, 'User 4')
                insert #Users values(5, 'User 5')
                insert #Users values(6, 'User 6')
                insert #Users values(7, 'User 7')
                insert #Users values(8, 'User 8')
                insert #Users values(9, 'User 9')

                insert #ReviewBoards values(1, 'Review Board 1', 1, 2, 3, 4, 5, 6, 7, 8, 9)
";

            using (var connection = Program.GetOpenConnection())
            {
                connection.ExecuteAsync(createSql).Wait();
                try
                {
                    var sql = @"
                    select 
                        rb.Id, rb.Name,
                        u1.*, u2.*, u3.*, u4.*, u5.*, u6.*, u7.*, u8.*, u9.*
                    from #ReviewBoards rb
                        inner join #Users u1 on u1.Id = rb.User1Id
                        inner join #Users u2 on u2.Id = rb.User2Id
                        inner join #Users u3 on u3.Id = rb.User3Id
                        inner join #Users u4 on u4.Id = rb.User4Id
                        inner join #Users u5 on u5.Id = rb.User5Id
                        inner join #Users u6 on u6.Id = rb.User6Id
                        inner join #Users u7 on u7.Id = rb.User7Id
                        inner join #Users u8 on u8.Id = rb.User8Id
                        inner join #Users u9 on u9.Id = rb.User9Id
";

                    var types = new[] { typeof(ReviewBoard), typeof(User), typeof(User), typeof(User), typeof(User), typeof(User), typeof(User), typeof(User), typeof(User), typeof(User) };

                    Func <object[], ReviewBoard> mapper = (objects) =>
                    {
                        var board = (ReviewBoard)objects[0];
                        board.User1 = (User)objects[1];
                        board.User2 = (User)objects[2];
                        board.User3 = (User)objects[3];
                        board.User4 = (User)objects[4];
                        board.User5 = (User)objects[5];
                        board.User6 = (User)objects[6];
                        board.User7 = (User)objects[7];
                        board.User8 = (User)objects[8];
                        board.User9 = (User)objects[9];
                        return(board);
                    };

                    var data = connection.QueryAsync <ReviewBoard>(sql, types, mapper).Result.ToList();

                    var p = data.First();
                    p.Id.IsEqualTo(1);
                    p.Name.IsEqualTo("Review Board 1");
                    p.User1.Id.IsEqualTo(1);
                    p.User2.Id.IsEqualTo(2);
                    p.User3.Id.IsEqualTo(3);
                    p.User4.Id.IsEqualTo(4);
                    p.User5.Id.IsEqualTo(5);
                    p.User6.Id.IsEqualTo(6);
                    p.User7.Id.IsEqualTo(7);
                    p.User8.Id.IsEqualTo(8);
                    p.User9.Id.IsEqualTo(9);
                    p.User1.Name.IsEqualTo("User 1");
                    p.User2.Name.IsEqualTo("User 2");
                    p.User3.Name.IsEqualTo("User 3");
                    p.User4.Name.IsEqualTo("User 4");
                    p.User5.Name.IsEqualTo("User 5");
                    p.User6.Name.IsEqualTo("User 6");
                    p.User7.Name.IsEqualTo("User 7");
                    p.User8.Name.IsEqualTo("User 8");
                    p.User9.Name.IsEqualTo("User 9");
                }
                finally
                {
                    connection.Execute("drop table #Users drop table #ReviewBoards");
                }
            }
        }