DataContext.Commit() Method
.NET Standard 2.x
Commits the database transaction.
Namespace: SnapObjects.Data
Assembly: SnapObjects.Data.dll
Syntax
public virtual void Commit();
Examples
The following code example how to commit the transaction.
using System;
namespace Appeon.ApiDoc.DataContextExamples
{
public class CommitExample
{
private readonly SchoolContext _context;
public CommitExample(SchoolContext context)
{
_context = context;
}
public void Example()
{
// Starts the transaction.
_context.BeginTransaction();
try
{
string sql = @"INSERT Department
(DepartmentId, Name,Budget,StartDate, Administrator)
VALUES (1, 'Engineering', 350000, '2007-09-01', 2)";
_context.SqlExecutor.Execute(sql);
// Attempts to commit the transaction.
_context.Commit();
Console.WriteLine("Record has been written to database.");
}
catch (Exception ex)
{
Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
Console.WriteLine("Message: {0}", ex.Message);
// Attempts to roll back the transaction.
try
{
_context.Rollback();
}
catch (Exception ex2)
{
// Handle any errors that may have occurred on the server that
// could cause the rollback to fail.
// Such as a closed connection.
Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
Console.WriteLine("Message: {0}", ex2.Message);
}
}
}
}
}
Applies to
.NET Standard
2.x