Connection string in WCF with LINQ, C #, VS2008

I have added a DBML file with the appropriate connection string and valid credentials. I registered my VPN server that hosts the SQL Server and I wanted to test my WCF service in terms of what errors would be thrown if it couldn't find the DB.

public List<Users> GetName(strinng UserEmail)
{

  var dbResult = from u in Users
                 where u.email.Equals(UserEmail)
                 select {v.Firstname, v.LastName, v.Zip};

  //Build List<Users>
  return List<users>;
}

      

Let's say above, this is one of my methods. When calling a method without accessing my DB, I didn't see an error thrown. How to check if the connection is valid and that the DB exists?

I assumed the DBML.cs file would provide this check in cstor

0


a source to share


2 answers


When I ran my little test mine I tried to do ToList () due to LINQ execution latency). I got a SqlException when the connection could not be made ... it took a little time for an exception to be thrown, but it inevitably happened.

here is my little LINQ test code:

TestDataContext con = new TestDataContext();
        var users = from user in con.Users
                    select user;
        //failed on this line...
        IList<User> faUsers = users.ToList();

      



My test was that I just shut down my Sql Server instance.

If you are talking about client side debugging of this web method call, the connection to the web method might be disconnected before the connection between the web service and the database server ... and it might be your timeout exception.

+1


a source


Even if that's not your question, you can make your method much simpler with something like this (untested):

  var dbResult = from u in db.Users
                 where u.email.Equals(UserEmail)
                 select new User() 
                 {
                     FirstName = u.Firstname, 
                     LastName = u.LastName, 
                     Zip = u.Zip
                 };

  return dbResult.ToList();

      



// update: modified typos copied from the original question

0


a source







All Articles