Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Discuss the importance of implementing transactions in web applications. Describ

ID: 3672509 • Letter: D

Question

Discuss the importance of implementing transactions in web applications. Describe the COMMIT, and ROLLBACK command. In the following SaveUser function below, add code to insert the first name, last name, and status into the tblUser table. In the database the column names are: FirstName, LastName, Status. Also, write the code to commit the transaction.


public static bool SaveUser(string Database, string FirstName, string LastName, string Status)
{
bool recordSaved;
try
{
OleDbTransaction myTransaction = null;
OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" +"Data Source=" +Database);
conn.Open();
OleDbCommand command = conn.CreateCommand();
string strSQL;


myTransaction = conn.BeginTransaction();
command.Transaction = myTransaction;


// Write the code below to insert the first name, last name, and status into the tblUser table
_____________________________________________________


// Add your comments here
command.CommandType = CommandType.Text;
command.CommandText = strSQL;


// Add your comments here
command.ExecuteNonQuery();


// Write the code below to commit the transaction
_______________________________________________________


// Add your comments here
conn.Close();


recordSaved = true;
}
catch (Exception ex)
{
myTransaction.Rollback();

recordSaved = false;
}




return recordSaved;


}

Explanation / Answer

public static bool SaveUser(string Database, string FirstName, string LastName, string Status)
{
bool recordSaved;
try
{
OleDbTransaction myTransaction = null;   'Creates a variable to represent an SQL transaction.'
OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;" +"Data Source=" +Database);   'Creates an open connection to a datasource.
conn.Open();   'Opens a database connection.'
OleDbCommand command = conn.CreateCommand();   'Represents an SQL statement to execute against a datasource.'
string strSQL;                   'A string variable.'

   
myTransaction = conn.BeginTransaction();   'Starts a database Transaction.'
command.Transaction = myTransaction;       'Gets or Sets OleDbTransaction with which command eecutes.'


// Write the code below to insert the first name, last name, and status into the tblUser table
_____________________________________________________
command = new OleDbCommand("INSERT INTO tblUser ([firstName], [lastName], [status]) VALUES (?, ?, ?)", conn, myTransaction);
//Initializes a new instance of the OldDbCommand with the query, conn, and myTransaction.


// Add your comments here
command.CommandType = CommandType.Text;
command.CommandText = strSQL;

//Executes the command, and will return the number of rows effected.
// Add your comments here
command.ExecuteNonQuery();


// Write the code below to commit the transaction
_______________________________________________________
myTransaction.Commit();   //Commits the transaction, which means it pushes the database to a consistent state.


// Add your comments here
conn.Close();


recordSaved = true;
}
catch (Exception ex)
{
myTransaction.Rollback();   //If some exception occurs, the recent updates which pushed the database to inconsistent state will be rolled back, and will move it back to the previous committed state.

recordSaved = false;
}


return recordSaved;


}

If you need any refinements, with this, just get back to me.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote