0

Okay, so I have this LINQ to SQL situation. I have a DataContext object which links to an SQL database and I can get tables and... all of that business. This is all inside a WCF service inside IIS.

Now I connect the DataContext object when I declare it, which is at the top of the class, as follows:

DataContext dc = new DataContext([ConnectionString]);

Then, later, I have methods to do things like inserting a Client (that is a type of entity), and that looks something like this:

public void InsertClient(Client client)
{
   if(client.ClientID > 0)
      return;

   dc.Clients.InsertOnSubmit(client);

   dc.SubmitChanges();
}

The problem is basically... is there some way to test that connection? What if someone pulled a plug or something before SubmitChanges? It's not like it returns anything, so it couldn't tell me there was a particular error.

How would people recommend I do this?

Thanks.

3 Answers 3

4

DataContext.SubmitChanges will throw if there is an error.

Sign up to request clarification or add additional context in comments.

1 Comment

Probably something like A network-related or instance-specific error occurred while establishing a connection to SQL Server
1
public bool InsertClient(Client client)
{
   if(client.ClientID > 0)
      return;

   dc.Clients.InsertOnSubmit(client);

   try
   {
      dc.SubmitChanges();
      return true;
   }
   catch
   {
        return false;
   }
}

Comments

0

Well, the datacontext has the public DataContext.Connection Property so if you want you can do anything you want with it...

But I do not think that is particually usefull since Submitchanges anyway will throw like is written in the other answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.