views:

867

answers:

2

How to check if Linq.DataContext object is filled with data and no connection error has appeared?

Thanks.

+7  A: 
DataContext.DatabaseExists()

or

DataContext.Connection.Open()
leppie
I must admit, I didn't see the relation between the question and the database existing; well deduced... psychic debugging ;-p
Marc Gravell
+2  A: 

What specific scenario are you trying to guard against?

A DataContext is not the same as a DataSet - it isn't (necessarily) a container of data (identity management / change tracking aside) - it is a source of data.

In general, it won't give you an object (from a query) if an error occurs - it'll throw an exception. So if you have been given an instance by LINQ-to-SQL, it should have something in it.

Are you seeing a specific problem that you want to avoid?

Marc Gravell
I wanted to avoid an exception. :)
Alexander Prokofyev
@Alexander -- Checking for existence or an open connection only helps if the database isn't available when you start using the context. It won't help if the DB server goes down while you are using the context. I'm afraid that you'll have to live with the possibility of exceptions anyway.
tvanfosson
@tvanfosson: I see. Thanks!
Alexander Prokofyev