exam questions

Exam 70-487 All Questions

View all questions & answers for the 70-487 exam

Exam 70-487 topic 1 question 22 discussion

Actual exam question from Microsoft's 70-487
Question #: 22
Topic #: 1
[All 70-487 Questions]

DRAG DROP -
You have an Azure SQL Database that contains two tables named Country and City.
You need to insert a country into the Country table and a city into the City table. The solution must meet the following requirements:
✑ If an error occurs while attempting to add the country, the city must NOT be added.
✑ If an error occurs while attempting to add the city, the country must NOT be added.
How should you complete the code? To answer, drag the appropriate code blocks to the correct locations in the answer area. Each code block may be used once, more than once, or not at all. You may need to drag the split bar between panes or scroll to view content.
Select and Place:

Show Suggested Answer Hide Answer
Suggested Answer:
References:
https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction(v=vs.110).aspx

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
tiger25
Highly Voted 5 years, 4 months ago
In this scenario the answer is correct. But on my exam the second condition was another: If an error occurs while attempting to add the city, the country must be added. So I answered: Rollback("Inserted") in the second block.
upvoted 22 times
UweJ
5 years, 3 months ago
Thank you for mentioning this variation.
upvoted 5 times
...
...
pepster
Highly Voted 5 years, 6 months ago
Тransaction is named "Beginning". "Inserted" is the name of save point. Since the condition requires that when an error occurs, no city or country to be added, then the transaction must be Rollback to the starting point, not to the save point. So the solution is correct.
upvoted 13 times
TinaZh
5 years, 4 months ago
Agreed. We need completely rollback the transaction. According to https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqltransaction.rollback?view=netframework-4.8#System_Data_SqlClient_SqlTransaction_Rollback_System_String_ transactionName - The name of the transaction to roll back, or the savepoint to which to roll back.
upvoted 1 times
...
mr_
4 years, 11 months ago
I agree with you. You are completely right IMO. We can't rollback to the save point because then country would be inserted and it shouldn't be according to requirements.
upvoted 3 times
...
...
northgaterebel
Most Recent 4 years, 5 months ago
Why bother to create a save point that we never roll back to? According to this article a save point "Allows you to create a save point so that you can roll back to a particular saved state of the database." https://www.c-sharpcorner.com/uploadfile/mahesh/rollback-commit-and-savepoints-in-ado-net/
upvoted 1 times
northgaterebel
4 years, 5 months ago
Nevermind. I get it now. Save is a best practice. If we rollback to save point "inserted" the country is already in the database, which we do not want in this case.
upvoted 1 times
...
...
apostolin
4 years, 6 months ago
using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = connection.CreateCommand(); command.Connection = connection; SqlTransaction transaction = connection.BeginTransaction("Beginning"); command.Transaction = transaction; try { command.CommandText = "INSERT INTO Country (CountryName) VALUES ('Country1')"; command.ExecuteNonQuery(); transaction.Save("Inserted"); try { command.CommandText = "INSERT INTO City (CityName) VALUES ('City1')"; command.ExecuteNonQuery(); transaction.Commit(); } catch (Exception ex) { transaction.Rollback("Inserted"); } } catch (Exception ex) { transaction.Rollback("Beginning"); } }
upvoted 1 times
...
TimboJones
4 years, 7 months ago
For everyone arguing about the Rollback to Inserted. What's the purpose of the savepoint if you never rollback to it? I believe the question is just wrong here and the "second" variant is the actual correct question. So rollback("inserted") might indeed be true after all
upvoted 2 times
...
tanujgyan
5 years, 1 month ago
As mentioned by Pepster the given solution is right
upvoted 1 times
...
sophronitis
5 years, 6 months ago
In my opion in the outermost catch block the transaction should be disposed because "beginning" is not a save point, it's the name of the transaction (https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection.-ctor?view=netframework-4.8) and at that point no save point exists.
upvoted 1 times
...
AlexFromGalax
5 years, 6 months ago
The last rollback also. If you name a transaction "inserted" you should rollback this transaction whether you roll it back in the first or the last catch block.
upvoted 1 times
...
Jagmohan
5 years, 6 months ago
In the second itme, it should be Rollback("Inserted")
upvoted 1 times
zimzimzimma
4 years, 9 months ago
There's a variant of this question in which the country must be added when the city insert gives an error. However, in this case the country must NOT be added. I think you are mixing up this question with the other variant. The given answer is correct as others have stated as well.
upvoted 2 times
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...