Question

I have been looking into using database transactions in the environment I am currently programming in (C#.Net WebPages with WebMatrix, using SQL Server CE), and have read up a lot on the topic.

At first, I was looking into this because I felt it was 100% necessary for my project, although, now I realize that the default "Last-in-wins" concurrency policy will work fine for my somewhat simple CRUD system.

Now, I don't have enough knowledge about this to really say that I, 100%, need this, but I would like to, if possible, converge all of the statements I would otherwise have to hit the database several times for, into one statement, so as to minimize the number of times the database has to be locked (again, I'm not sure if this is worth worrying over, but it just feels like a single statement would cleaner, more efficient, and possibly even less error-prone).

In my server-side code, I have some logical branches that, depending on whether ID numbers can be reused or not, needs to perform an INSERT INTO or an UPDATE statement to the database for each row that I need to add. Problem is there is no way, other than transactions, to combine multiple UPDATE and/or INSERT statements into one communicable statement.

Lastly, in reviewing transaction syntax for my environment myself, I came across many pages that describe how transactions work, but provide no syntactical examples thereof. I did find this page...:

http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlcecommand.transaction(v=vs.100).aspx

...that actually targets my environment and server-side code, exactly, however, the syntax used does not show up with Intellisence as I'm typing, which has always been a tell-tale sign that the syntax is wrong. In this page, though, I can easily see how logically I should set this up (using 'try', 'catch', and even 'finally'), and that makes perfect sense to me. My problem is that I can't find any syntax for my given environment on how to actually compile a transaction.

Is there anyone who could possibly provide an example for the syntax needed to compile a database transaction with multiple INSERT and UPDATE statements, within my C#.Net WebPages with WebMatrix, using SQL Server CE, environment?

Was it helpful?

Solution

Transactions do not provide a mechanism for reducing the number of commands that are executed against the database. You do that by batching statements - using the semi colon as a statement separator:

"INSERT INTO table1 VALUES (x,y,z); INSERT INTO table2 VALUES(1,2,3)"

Then you can execute the lot in one shot:

command.ExecuteNonQuery(sql);

That results in one call to the database, achieving your objective.

HOWEVER:

SQL Compact doesn't support batched statements. So you cannot use them. You have to execute each statement in turn.

You really do not need to concern yourself with database locking when an insert is being performed. It takes a fraction of a nanosecond, and the database itself will take care or queueing requests so that users are unaffected.

OTHER TIPS

Did you add System.Transactions dll to your references ? that of of course if you are using TransactionScope but if you are using IDbTransaction you should just add using System.Data;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top