Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. public void Add(Account newAccount)
  2. {
  3. //throw new NotImplementedException();
  4. connection = icon.CreateSqlConnection();
  5. SqlTransaction transaction = null;
  6. Account account = new Account();
  7.  
  8. string insertQueryAccount = "INSERT INTO Accounts (Id, AccountNumber, Balance, AccounType, CustomerId)" +
  9. "VALUES (@id, @accountNumber, @balance, @accountType, @customerId);" +
  10. "SELECT scope_identity()";
  11.  
  12. SqlCommand insertAccount = new SqlCommand(insertQueryAccount, connection);
  13. insertAccount.Parameters.AddWithValue("@accountNumber", newAccount.AccountNumber);
  14. insertAccount.Parameters.AddWithValue("@balance", newAccount.Balance);
  15. insertAccount.Parameters.AddWithValue("@accountType", newAccount.AccountType);
  16. insertAccount.Parameters.AddWithValue("@customerId", newAccount.CustomerId);
  17.  
  18. try
  19. {
  20. connection.Open();
  21. transaction = connection.BeginTransaction();
  22. insertAccount.Transaction = transaction;
  23. account.Id = Convert.ToInt32(insertAccount.ExecuteScalar());
  24. }
  25. catch (Exception)
  26. {
  27. transaction.Rollback();
  28. }
  29. finally
  30. {
  31. connection?.Close();
  32. }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement