How can I go about Implementing Database transactions for SQL Server CE in a C#.Net Web-Pages w/ WebMatrix Environment?

StackOverflow https://stackoverflow.com/questions/19645185

Question

It has just come to my knowledge two important things considering the database I have that serves up web-page content, while simultaneously allowing a select few users to edit said pages.

  1. Database transactions are required.
  2. Database transactions might be incredibly complicated (from my C#.NET Web-Pages w/ WebMatrix environment's perspective).

So, I Wikipedia'd, Google'd, and even SO'd for even a small ray of hope on how to go about understanding how to simply get started with this, so that my database may be consistent.

After reading up on this topic (including this page: http://technet.microsoft.com/en-us/library/ms172400.aspx), I have come to realize that while I am learning a bit more on transactions, I feel NO closer to actually implementing them myself, for my own SQL Server CE database.

I realize (at least somewhat) the magnitude of what I'm asking but was hoping someone who understands my environment (C#.NET Web-Pages w/ WebMatrix, using SQL Server CE) could perhaps provide an example of how this might be accomplished.

I don't need a whole lot of fancy stuff or anything, I just want to make sure that no one hits the database while saved changes are being committed (well, what I think I mean to say is that I want the users to receive the intended content, not crash the database if two users save at the same time, etc.).

Let's say I have a single table [testTable] with two columns [objectID, value].

I have a simple select statement [say: "SELECT * FROM testTable"] then I populate a page with it, whenever anyone visits it.

Now, say I have another page that a select few users can modify the viewable page with, then when they click "Save" it saves over the older values with something like ["UPDATE testTable SET objectID = @0, value = @1"]

Considering that I don't know how to begin the syntax for either the ADO.NET method, nor the straight SQL method, which is easiest considering my environment and what would the syntax be?

Was it helpful?

Solution

Your issue is not one that transactions solve. Transactions act as a wrapper around a number of operations, which either all succeed or all fail when you attempt to commit them. The problem you are trying to solve is one of concurrency. You just need to decide on a concurrency strategy. "Last in wins" is usually good enough for most CRUD systems - there is normally only one version of the truth, but your situation may require a different approach.

Other strategies are Optimistic Concurrency and Pessimistic concurrency:

From this page (http://msdn.microsoft.com/en-us/library/cs6hb8k4(v=vs.80).aspx)

Types of Concurrency Control

In general, there are three common ways to manage concurrency in a database:

  • Pessimistic concurrency control: A row is unavailable to users from the time the record is fetched until it is updated in the database.
  • Optimistic concurrency control: A row is unavailable to other users only while the data is actually being updated. The update examines
    the row in the database and determines whether any changes have been
    made. Attempting to update a record that has already been changed
    results in a concurrency violation.
  • "Last in wins": A row is unavailable to other users only while the data is actually being updated. However, no effort is made to compare updates against the original record; the record is simply written out, potentially overwriting any changes made by other users since
    you last refreshed the records.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top