C# – Linq To Sql – Handle a Transaction Deadlock

When using Linq To SQL, here’s the pattern I use to retry an operation after it failed because a deadlock occured in a SQL Database: while (true) { try { using (var scope = new TransactionScope()) { using (var db = new MyDataContext()) { // Do what you need to do on the DB… db.SubmitChanges(); … Read moreC# – Linq To Sql – Handle a Transaction Deadlock

C# – Linq to Sql – Reattach and Update an entity

If you’d listen to Microsoft, you’d believe than in Linq to SQL, you can’t reattach a changed entity to a new DataContext and submit the changed entity to the database. Well this isn’t true. It can be done with a little bit of foresight. The first step is to make sure that the underlying table … Read moreC# – Linq to Sql – Reattach and Update an entity

C# : How to Wrap Winword COM InterOp Methods

If you have ever used the Microsoft Office COM Interop classes to automate Word (or any other Office Application), you know those methods often take a lot of parameters, which can be tedious. Note: This was written a while ago before .Net 4.0 was available. .Net 4.0 introduces the dynamic keyword which makes COM Interop … Read moreC# : How to Wrap Winword COM InterOp Methods

C#: Use verbatim strings for SQL queries

Using verbatim strings for SQL queries can make your code much more readable. For example, which one of the following queries is easier to read? This one? string query = @” SELECT User.FirstName, User.LastName, User.Age, User.Gender, User.HairColor, Adress.Street, Adresse.Zip, Adress.City, Adress.Country FROM User INNER JOIN Adress ON User.AdressId = Adress.Id SORT BY User.FirstName, User.LastName “; … Read moreC#: Use verbatim strings for SQL queries