Execute a command multiple times

Aug 1, 2012 at 1:18 PM

Is there a way to execute a command multiple times with different parameters? For instance, I have a parent object with a child collection. I'd want to insert a row into the ParentChild join table n (=collection.Count()) number of times. Can I do it with a single expression? Without having to grab a Context() each time?

Aug 2, 2012 at 1:26 PM

Hi, today this is not possible. Since you will be doing multiple inserts I suggest you use a transaction, then if any error occur then you can rollback all the changes. For more details see http://fluentdata.codeplex.com/documentation#Transactions.

Aug 7, 2012 at 6:46 AM

Hi

 

Once transaction is committed, the connection is auto closed?

How to check connection status whether it's closed or open?

Aug 7, 2012 at 11:18 PM

No, the connection is closed when Dispose is called. When you use using() then Dispose is automatically called. If you use try and finally instead then you need to make sure that Dispose is called.

using (var context = Context().UseTransaction(true))
{
	context.Sql("update Product set Name = @0 where ProductId = @1")
				.Parameters("The Warren Buffet Way", 1)
				.Execute();

	context.Sql("update Product set Name = @0 where ProductId = @1")
				.Parameters("Bill Gates Bio", 2)
				.Execute();

	context.Commit();
}

or

var context = Context().UseTransaction(true)
try
{
	context.Sql("update Product set Name = @0 where ProductId = @1")
				.Parameters("The Warren Buffet Way", 1)
				.Execute();

	context.Sql("update Product set Name = @0 where ProductId = @1")
				.Parameters("Bill Gates Bio", 2)
				.Execute();

	context.Commit();
}
finally
{
	context.Dispose();
}