Пример #1
0
        static async Task Sample13Async()
        {
            Employee[] newEmployees = new Employee[] {
                new Employee() { CompanyID = 1, Name = "New Employee1", Age = 23, StartWorking = DateTime.UtcNow },
                new Employee() { CompanyID = 1, Name = "New Employee2", StartWorking = DateTime.UtcNow },
                new Employee() { CompanyID = 2, Name = "New Employee1" }
            };

            var simpleQuery = "select * from Employee where Age is not null";

            // mapped command
            var precompiled2 = MappedCommand.Compile<Employee>("update Employee set Age = Age + 1 where ID = @ID");

            // using method SqlScope.UsingTransaction
            await SqlScope.UsingTransactionAsync(connStr, async scope =>
            {
                // using simple command
                var arr = await scope.CreateSimple(simpleQuery).ExecuteQueryAsync<Employee>().ToArray();

                // using precompiled mapped command
                Employee e = new Employee() { ID = 3 };
                await scope.CreateMapped(precompiled2, e).ExecuteNonQueryAsync();
                e.ID = 4;
                await scope.CreateMapped(precompiled2, e).ExecuteNonQueryAsync();

                await newEmployees.WriteToServerAsync(scope.Transaction, "Employee");
            });

            // using external transaction
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                await conn.OpenAsync();

                var tran = conn.BeginTransaction();

                try
                {
                    SqlScope scope = new SqlScope(tran);

                    // using simple command
                    var arr = await scope.CreateSimple(simpleQuery).ExecuteQueryAsync<Employee>().ToArray();

                    // using precompiled mapped command
                    Employee e = new Employee() { ID = 3 };
                    await scope.CreateMapped(precompiled2, e).ExecuteNonQueryAsync();
                    e.ID = 4;
                    await scope.CreateMapped(precompiled2, e).ExecuteNonQueryAsync();

                    await newEmployees.WriteToServerAsync(scope.Transaction, "Employee");

                    tran.Commit();
                }
                catch
                {
                    tran.Rollback();
                    throw;
                }
            }
        }
Пример #2
0
        static async Task Sample11Async()
        {
            Employee[] newEmployees = new Employee[] {
                new Employee() { CompanyID = 1, Name = "New Employee11", Age = 23, StartWorking = DateTime.UtcNow },
                new Employee() { CompanyID = 1, Name = "New Employee21", StartWorking = DateTime.UtcNow },
                new Employee() { CompanyID = 2, Name = "New Employee11" }
            };

            await newEmployees.WriteToServerAsync(connStr, "Employee");
        }