This is a quick post for my own ignorance. On occasion I use LinqPad to make changes in a database and I find the stock LinqPad linq to sql provider to be very useful. I just happen to forget the syntax when making changes. This isn’t limited to just database operations, there are other things I just can’t remember to save my life.

Saving to database

//Using the target DB Set, call the "InsertOnSubmit" method like so:
Persons.InsertOnSubmit(new Person());

//Finally call the SubmitChanges method to execute
SubmitChanges();

Executing in a transaction

https://stackoverflow.com/questions/11126025/rollback-inserts-updates-deletes-in-linq-pad

//Set the transaction options otherwise the default is Serializable which is awful to use
var options = new TransactionOptions();
options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
options.Timeout = new TimeSpan(0, 5, 0); //Five minutes is plenty

//Setup the transaction with options
using (var scope = new TransactionScope(TransactionScopeOption.Required, options))
{
    //Perform your data saving operations here

    //If an exception is thrown, then a rollback will automatically occurr
    //Call the Complete method to commit transaction
    scope.Complete();
}

Get your current execution path

//I use this all the time and I forget what the syntax is because it's LinqPad specific
var path = Path.GetDirectoryName(Util.CurrentQueryPath);

Leave a Reply

Your email address will not be published. Required fields are marked *