// Using transaction static void Sample13() { 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 SqlScope.UsingTransaction(connStr, scope => { // using simple command var arr = scope.CreateSimple(simpleQuery).ExecuteQuery<Employee>().ToArray(); // using precompiled mapped command Employee e = new Employee() { ID = 3 }; scope.CreateMapped(precompiled2, e).ExecuteNonQuery(); e.ID = 4; scope.CreateMapped(precompiled2, e).ExecuteNonQuery(); newEmployees.WriteToServer(scope.Transaction, "Employee"); }); // using external transaction using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); var tran = conn.BeginTransaction(); try { SqlScope scope = new SqlScope(tran); // using simple command var arr = scope.CreateSimple(simpleQuery).ExecuteQuery<Employee>().ToArray(); // using precompiled mapped command Employee e = new Employee() { ID = 3 }; scope.CreateMapped(precompiled2, e).ExecuteNonQuery(); e.ID = 4; scope.CreateMapped(precompiled2, e).ExecuteNonQuery(); newEmployees.WriteToServer(scope.Transaction, "Employee"); tran.Commit(); } catch { tran.Rollback(); throw; } } }
// Bulk insert of entities to database static void Sample11() { 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" } }; newEmployees.WriteToServer(connStr, "Employee"); }