{"id":3628,"date":"2024-05-13T16:33:21","date_gmt":"2024-05-13T09:33:21","guid":{"rendered":"https:\/\/www.sqlitetutorial.net\/?page_id=3628"},"modified":"2024-10-25T20:05:17","modified_gmt":"2024-10-25T13:05:17","slug":"transaction","status":"publish","type":"page","link":"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/","title":{"rendered":"SQLite C#: Transactions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to perform database transactions in the SQLite database  using ADO.NET in a C# program<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to database transactions<\/h2>\n\n\n\n<p>In SQLite, a transaction consists of multiple SQL statements that execute in an all-or-nothing manner:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If any statement fails, changes made to the database are rolled back. <\/li>\n\n\n\n<li>If all statements succeed, the changes are committed to the database permanently.<\/li>\n<\/ul>\n\n\n\n<p>To perform a transaction in a C# program using ADO.NET, you follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, <a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/connect\/\">open a new database connection<\/a>.<\/li>\n\n\n\n<li>Next, start a new transaction by calling the <code>BeginTransaction()<\/code> method of the <code>SqliteConnection<\/code> objects. It returns a <code>SqliteTransaction<\/code> object.<\/li>\n\n\n\n<li>Then, execute multiple SQL statements.<\/li>\n\n\n\n<li>After that, commit the changes to the database by calling the <code>Commit()<\/code> method of the <code>SqliteTransaction<\/code> object.<\/li>\n\n\n\n<li>Finally, roll back the changes since the transaction started if an error occurs by calling the <code>Rollback()<\/code> method of the <code>SqliteTransaction<\/code> object. <\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Transaction examples<\/h2>\n\n\n\n<p>Let&#8217;s take an example of performing a transaction in the SQLite database using C# ADO.NET.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Creating new tables<\/h3>\n\n\n\n<p>First, open the Terminal on Unix-like systems or Command Prompt on Windows.<\/p>\n\n\n\n<p>Second, connect to the <code>C:\\db\\pub.db<\/code> using the sqlite3 client program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">sqlite3 c:\\db\\pub.db<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, <a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-create-table\/\">create a new table<\/a> called <code>books<\/code> to store the book data<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-function\">CREATE TABLE <span class=\"hljs-title\">books<\/span>(<span class=\"hljs-params\">\n    id INTEGER PRIMARY KEY, \n    title TEXT NOT NULL,\n    isbn TEXT NOT NULL,\n    published_date DATE NOT NULL\n<\/span>)<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, <a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-create-table\/\">create a new table<\/a> called <code>book_authors<\/code> that stores the relationship between books and authors:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-function\">CREATE TABLE <span class=\"hljs-title\">book_authors<\/span>(<span class=\"hljs-params\">\n    book_id INT,\n    author_id INT,\n    PRIMARY KEY(book_id, author_id<\/span>),\n    FOREIGN <span class=\"hljs-title\">KEY<\/span> (<span class=\"hljs-params\">book_id<\/span>) REFERENCES <span class=\"hljs-title\">books<\/span>(<span class=\"hljs-params\">id<\/span>) ON DELETE CASCADE,\n    FOREIGN <span class=\"hljs-title\">KEY<\/span> (<span class=\"hljs-params\">author_id<\/span>) REFERENCES <span class=\"hljs-title\">authors<\/span>(<span class=\"hljs-params\">id<\/span>) ON DELETE CASCADE\n)<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fifth, <a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-show-tables\/\">display all the tables<\/a> in the <code>pub<\/code> database:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">.tables<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">authors       book_authors  books<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Sixth, exit the sqlite3 program by running the following command and pressing the Enter (or Return) key:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">.quit<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">2) Performing a transaction<\/h3>\n\n\n\n<p>The following program inserts a new book into the <code>books<\/code> table and also inserts a new row into the <code>book_authors<\/code> table within the same transaction:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> Microsoft.Data.Sqlite;\n\n<span class=\"hljs-comment\">\/\/ Insert a new book<\/span>\n<span class=\"hljs-keyword\">var<\/span> title = <span class=\"hljs-string\">\"Harry Potter and the Philosopher's Stone\"<\/span>;\n<span class=\"hljs-keyword\">var<\/span> isbn = <span class=\"hljs-string\">\"9781408855652\"<\/span>;\n<span class=\"hljs-keyword\">var<\/span> publishedDate = <span class=\"hljs-keyword\">new<\/span> DateOnly(<span class=\"hljs-number\">1997<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">26<\/span>);\n\n\n<span class=\"hljs-keyword\">try<\/span>\n{\n    <span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">var<\/span> connection = <span class=\"hljs-keyword\">new<\/span> SqliteConnection(<span class=\"hljs-string\">@\"Data Source=C:\\db\\pub.db\"<\/span>);\n    connection.Open();\n    \n    <span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">var<\/span> transaction = connection.BeginTransaction();\n    <span class=\"hljs-keyword\">try<\/span>\n    {\n        <span class=\"hljs-keyword\">var<\/span> sql = <span class=\"hljs-string\">\"INSERT INTO books(title, isbn, published_date) VALUES(@title, @isbn, @published_date)\"<\/span>;\n        <span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">var<\/span> cmdInsertBook = <span class=\"hljs-keyword\">new<\/span> SqliteCommand(sql, connection, transaction);\n\n        cmdInsertBook.Parameters.AddWithValue(<span class=\"hljs-string\">\"@title\"<\/span>, title);\n        cmdInsertBook.Parameters.AddWithValue(<span class=\"hljs-string\">\"@isbn\"<\/span>, isbn);\n        cmdInsertBook.Parameters.AddWithValue(<span class=\"hljs-string\">\"@published_date\"<\/span>, publishedDate);\n\n        cmdInsertBook.ExecuteNonQuery();\n\n        <span class=\"hljs-comment\">\/\/ Get the last inserted book id<\/span>\n        sql = <span class=\"hljs-string\">\"SELECT last_insert_rowid()\"<\/span>;\n        <span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">var<\/span> cmdGetId= <span class=\"hljs-keyword\">new<\/span> SqliteCommand(sql, connection, transaction);\n        <span class=\"hljs-keyword\">var<\/span> bookId = cmdGetId.ExecuteScalar();\n        Console.WriteLine(<span class=\"hljs-string\">$\"Book id <span class=\"hljs-subst\">{bookId}<\/span>\"<\/span>);\n\n\n        <span class=\"hljs-comment\">\/\/ Assign book to author<\/span>\n        <span class=\"hljs-keyword\">var<\/span> authorId = <span class=\"hljs-number\">1<\/span>;\n        sql = <span class=\"hljs-string\">\"INSERT INTO book_authors(book_id, author_id) VALUES(@book_id, @author_id)\"<\/span>;\n        <span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">var<\/span> cmdInsertBookAuthor  = <span class=\"hljs-keyword\">new<\/span> SqliteCommand(sql, connection, transaction);\n\n        cmdInsertBookAuthor.Parameters.AddWithValue(<span class=\"hljs-string\">\"@book_id\"<\/span>, bookId);\n        cmdInsertBookAuthor.Parameters.AddWithValue(<span class=\"hljs-string\">\"@author_id\"<\/span>, authorId);\n\n        cmdInsertBookAuthor.ExecuteNonQuery();\n\n        <span class=\"hljs-comment\">\/\/ Commit the transaction<\/span>\n        transaction.Commit();\n    }\n    <span class=\"hljs-keyword\">catch<\/span> (SqliteException e) {\n        <span class=\"hljs-comment\">\/\/ Roll back the transaction<\/span>\n        transaction.Rollback();\n        Console.WriteLine(e.Message);\n    }\n}\n<span class=\"hljs-keyword\">catch<\/span> (SqliteException ex)\n{\n    Console.WriteLine(ex.Message);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, open a connection to the SQLite database file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">var<\/span> connection = <span class=\"hljs-keyword\">new<\/span> SqliteConnection(<span class=\"hljs-string\">@\"Data Source=C:\\db\\pub.db\"<\/span>);\nconnection.Open();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, begin a transaction by calling the <code>BeginTransaction()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">var<\/span> transaction = connection.BeginTransaction();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, insert a new book into the books table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">var<\/span> sql = <span class=\"hljs-string\">\"INSERT INTO books(title, isbn, published_date) VALUES(@title, @isbn, @published_date)\"<\/span>;\n<span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">var<\/span> cmdInsertBook = <span class=\"hljs-keyword\">new<\/span> SqliteCommand(sql, connection, transaction);\n\ncmdInsertBook.Parameters.AddWithValue(<span class=\"hljs-string\">\"@title\"<\/span>, title);\ncmdInsertBook.Parameters.AddWithValue(<span class=\"hljs-string\">\"@isbn\"<\/span>, isbn);\ncmdInsertBook.Parameters.AddWithValue(<span class=\"hljs-string\">\"@published_date\"<\/span>, publishedDate);\n\ncmdInsertBook.ExecuteNonQuery();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, get the inserted book Id for inserting into the <code>book_authors<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Get the last inserted book id<\/span>\nsql = <span class=\"hljs-string\">\"SELECT last_insert_rowid()\"<\/span>;\n<span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">var<\/span> cmdGetId = <span class=\"hljs-keyword\">new<\/span> SqliteCommand(sql, connection, transaction);\n<span class=\"hljs-keyword\">var<\/span> bookId = cmdGetId.ExecuteScalar();\nConsole.WriteLine(<span class=\"hljs-string\">$\"Book id <span class=\"hljs-subst\">{bookId}<\/span>\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fifth, insert a new row into the <code>book_authors<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Assign book to author<\/span>\n<span class=\"hljs-keyword\">var<\/span> authorId = <span class=\"hljs-number\">1<\/span>;\nsql = <span class=\"hljs-string\">\"INSERT INTO book_authors(book_id, author_id) VALUES(@book_id, @author_id)\"<\/span>;\n<span class=\"hljs-keyword\">using<\/span> <span class=\"hljs-keyword\">var<\/span> cmdInsertBookAuthor = <span class=\"hljs-keyword\">new<\/span> SqliteCommand(sql, connection, transaction);\n\ncmdInsertBookAuthor.Parameters.AddWithValue(<span class=\"hljs-string\">\"@book_id\"<\/span>, bookId);\ncmdInsertBookAuthor.Parameters.AddWithValue(<span class=\"hljs-string\">\"@author_id\"<\/span>, authorId);\n\ncmdInsertBookAuthor.ExecuteNonQuery();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Sixth, commit the transaction:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-comment\">\/\/ Commit the transaction<\/span>\ntransaction.Commit();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, roll back the transaction if any exception occurs.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">transaction.Rollback();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">3) Verifying the transaction<\/h3>\n\n\n\n<p>First, open the Terminal on Unix-like systems or Command Prompt on Windows.<\/p>\n\n\n\n<p>Second, connect to the <code>C:\\db\\pub.db<\/code> using the sqlite3 client program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">sqlite3 c:\\db\\pub.db<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, retrieve data from the <code>books<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">SELECT * FROM books;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">id  title                                     isbn           published_date\n--  ----------------------------------------  -------------  --------------\n<span class=\"hljs-number\">1<\/span>   Harry Potter and the Philosopher<span class=\"hljs-string\">'s Stone  9781408855652  1997-06-26<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, retrieve data from the <code>book_authors<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">SELECT * FROM book_authors;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">book_id  author_id\n-------  ---------\n<span class=\"hljs-number\">1<\/span>        <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Call the <code>BeginTransaction()<\/code> method of the <code>SqliteConnection<\/code> object to start a transaction.<\/li>\n\n\n\n<li>Call the <code>Commit()<\/code> method to commit the transaction.<\/li>\n\n\n\n<li>Call the <code>Rollback()<\/code> method to roll back the transaction.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3628\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/\"\n\t\t\t\tdata-post-title=\"SQLite C#: Transactions\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3628\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/\"\n\t\t\t\tdata-post-title=\"SQLite C#: Transactions\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to perform a database transaction in the SQLite database from a C# program using ADO.NET.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3562,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3628","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQLite C#: Transactions<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to perform database transactions in the SQLite database from a C# program using ADO.NET.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQLite C#: Transactions\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to perform database transactions in the SQLite database from a C# program using ADO.NET.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/\" \/>\n<meta property=\"og:site_name\" content=\"SQLite Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-25T13:05:17+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/#\/schema\/person\/6d69b968cad0102e30d6694ed8dc6427\"},\"headline\":\"SQLite C#: Transactions\",\"datePublished\":\"2024-05-13T09:33:21+00:00\",\"dateModified\":\"2024-10-25T13:05:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/\"},\"wordCount\":391,\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/\",\"url\":\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/\",\"name\":\"SQLite C#: Transactions\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/#website\"},\"datePublished\":\"2024-05-13T09:33:21+00:00\",\"dateModified\":\"2024-10-25T13:05:17+00:00\",\"description\":\"In this tutorial, you will learn how to perform database transactions in the SQLite database from a C# program using ADO.NET.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlitetutorial.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQLite C#\",\"item\":\"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQLite C#: Transactions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/#website\",\"url\":\"https:\/\/www.sqlitetutorial.net\/\",\"name\":\"SQLite Tutorial\",\"description\":\"A Step-by-step SQLite Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlitetutorial.net\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/#\/schema\/person\/6d69b968cad0102e30d6694ed8dc6427\",\"name\":\"admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQLite C#: Transactions","description":"In this tutorial, you will learn how to perform database transactions in the SQLite database from a C# program using ADO.NET.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/","og_locale":"en_US","og_type":"article","og_title":"SQLite C#: Transactions","og_description":"In this tutorial, you will learn how to perform database transactions in the SQLite database from a C# program using ADO.NET.","og_url":"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/","og_site_name":"SQLite Tutorial","article_modified_time":"2024-10-25T13:05:17+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/#article","isPartOf":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/"},"author":{"name":"admin","@id":"https:\/\/www.sqlitetutorial.net\/#\/schema\/person\/6d69b968cad0102e30d6694ed8dc6427"},"headline":"SQLite C#: Transactions","datePublished":"2024-05-13T09:33:21+00:00","dateModified":"2024-10-25T13:05:17+00:00","mainEntityOfPage":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/"},"wordCount":391,"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/","url":"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/","name":"SQLite C#: Transactions","isPartOf":{"@id":"https:\/\/www.sqlitetutorial.net\/#website"},"datePublished":"2024-05-13T09:33:21+00:00","dateModified":"2024-10-25T13:05:17+00:00","description":"In this tutorial, you will learn how to perform database transactions in the SQLite database from a C# program using ADO.NET.","breadcrumb":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/transaction\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlitetutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQLite C#","item":"https:\/\/www.sqlitetutorial.net\/sqlite-csharp\/"},{"@type":"ListItem","position":3,"name":"SQLite C#: Transactions"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlitetutorial.net\/#website","url":"https:\/\/www.sqlitetutorial.net\/","name":"SQLite Tutorial","description":"A Step-by-step SQLite Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlitetutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlitetutorial.net\/#\/schema\/person\/6d69b968cad0102e30d6694ed8dc6427","name":"admin"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages\/3628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/comments?post=3628"}],"version-history":[{"count":5,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages\/3628\/revisions"}],"predecessor-version":[{"id":3764,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages\/3628\/revisions\/3764"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages\/3562"}],"wp:attachment":[{"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/media?parent=3628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}