Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void Add(Account newAccount)
- {
- //throw new NotImplementedException();
- connection = icon.CreateSqlConnection();
- SqlTransaction transaction = null;
- Account account = new Account();
- string insertQueryAccount = "INSERT INTO Accounts (Id, AccountNumber, Balance, AccounType, CustomerId)" +
- "VALUES (@id, @accountNumber, @balance, @accountType, @customerId);" +
- "SELECT scope_identity()";
- SqlCommand insertAccount = new SqlCommand(insertQueryAccount, connection);
- insertAccount.Parameters.AddWithValue("@accountNumber", newAccount.AccountNumber);
- insertAccount.Parameters.AddWithValue("@balance", newAccount.Balance);
- insertAccount.Parameters.AddWithValue("@accountType", newAccount.AccountType);
- insertAccount.Parameters.AddWithValue("@customerId", newAccount.CustomerId);
- try
- {
- connection.Open();
- transaction = connection.BeginTransaction();
- insertAccount.Transaction = transaction;
- account.Id = Convert.ToInt32(insertAccount.ExecuteScalar());
- }
- catch (Exception)
- {
- transaction.Rollback();
- }
- finally
- {
- connection?.Close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement