Guest User

Untitled

a guest
Aug 17th, 2018
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. FbTransaction dbtran = conn.BeginTransaction();
  2. FbCommand insertCommand = new FbCommand();
  3. insertCommand.CommandText = "insert into teachers(id_teacher, fio, post) values(@CodeOfTeacher, @FIO, @Post)";
  4. insertCommand.Connection = conn;
  5. FbCommand insertCommand2 = new FbCommand();
  6. insertCommand2.CommandText = "insert into teachersanddepartments(id_teacher, id_department) values(@CodeOfTeacher, @DepartmentCodeOfDepartment)";
  7. insertCommand2.Connection = conn;
  8. insertCommand.Transaction = dbtran;
  9. insertCommand2.Transaction = dbtran;
  10. insertCommand.Parameters.AddWithValue("@CodeOfTeacher", teacher.CodeOfTeacher);
  11. insertCommand.Parameters.AddWithValue("@FIO", teacher.FIO);
  12. insertCommand.Parameters.AddWithValue("@Post", teacher.Post);
  13.  
  14. insertCommand2.Parameters.AddWithValue("@CodeOfTeacher", teacher.CodeOfTeacher);
  15. insertCommand2.Parameters.AddWithValue("@DepartmentCodeOfDepartment", teacher.Department.CodeOfDepartment);
  16. try
  17. {
  18. int result = insertCommand.ExecuteNonQuery();
  19. int result_2 = insertCommand2.ExecuteNonQuery();
  20. dbtran.Commit();
  21. insertCommand.Dispose();
  22. insertCommand2.Dispose();
  23. return result > 0 && result_2 > 0;
  24. }
  25. catch (Exception e)
  26. {
  27. MessageBox.Show(e.Message);
  28. dbtran.Rollback();
  29. return false;
  30. }
  31.  
  32.  
  33.  
  34.  
  35. //using (FbTransaction dbtran = conn.BeginTransaction())
  36. //{
  37. // try
  38. // {
  39. // using (FbCommand insertCommand = new FbCommand())
  40. // {
  41. // insertCommand.CommandText = "EXECUTE BLOCK AS BEGIN " +
  42. // " EXECUTE STATEMENT 'insert into teachers (id_teacher, fio, post) values(@CodeOfTeacher, @FIO, @Post)';" +
  43. // " EXECUTE STATEMENT 'insert into teachersanddepartments (id_teacher, id_department) values((@CodeOfTeacher, @DepartmentCodeOfDepartment))';" +
  44. // "END";
  45. // insertCommand.Connection = conn;
  46. // insertCommand.Transaction = dbtran;
  47.  
  48. // insertCommand.Parameters.AddWithValue("@CodeOfTeacher", teacher.CodeOfTeacher);
  49. // insertCommand.Parameters.AddWithValue("@FIO", teacher.FIO);
  50. // insertCommand.Parameters.AddWithValue("@Post", teacher.Post);
  51. // insertCommand.Parameters.AddWithValue("@DepartmentCodeOfDepartment", teacher.Department.CodeOfDepartment);
  52.  
  53. // int result = insertCommand.ExecuteNonQuery();
  54. // dbtran.Commit();
  55. // return result > 0;
  56. // }
  57. // }
  58. // catch(Exception e)
  59. // {
  60. // MessageBox.Show(e.Message);
  61. // dbtran.Rollback();
  62. // return false;
  63. // }
  64. //}
RAW Paste Data