Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF中带有LINQ、C、VS2008的连接字符串_C#_Linq_Wcf - Fatal编程技术网

C# WCF中带有LINQ、C、VS2008的连接字符串

C# WCF中带有LINQ、C、VS2008的连接字符串,c#,linq,wcf,C#,Linq,Wcf,我添加了一个具有适当连接字符串和有效凭据的DBML文件。我记录了我的承载SQL server的VPN,我想测试我的WCF服务,看看如果找不到数据库会出现什么错误 public List<Users> GetName(strinng UserEmail) { var dbResult = from u in Users where u.email.Equals(UserEmail) select {v.First

我添加了一个具有适当连接字符串和有效凭据的DBML文件。我记录了我的承载SQL server的VPN,我想测试我的WCF服务,看看如果找不到数据库会出现什么错误

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>;
}
说以上是我的方法之一。在调用没有访问我的数据库的方法时,我没有看到抛出任何错误。如何检查连接是否有效以及数据库是否存在


我假设DBML.cs文件将确保cstor中的检查,即使这不是您的问题,您也可以使用以下未经测试的内容使您的方法更简单:

  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();

//更新:当我运行我的小测试时,更改了从原始问题复制的打字错误。当我试图做一个ToList时,我的测试失败了,这是因为LINQ的延迟执行。当连接无法建立时,我得到了一个SqlException…异常需要一段时间才能返回,但它不可避免地发生了

以下是我的小测试LINQ代码:

TestDataContext con = new TestDataContext();
        var users = from user in con.Users
                    select user;
        //failed on this line...
        IList<User> faUsers = users.ToList();
我的测试是,我刚刚关闭了我的Sql Server实例


如果您谈论的是从此webmethod调用的客户端进行调试,那么到webmethod的连接可能在webservice和数据库服务器之间的连接之前超时……这可能是您的超时异常。

猜测用户是引用db上下文的属性?CSharpAtl:您是正确的。Users是一个表,可以通过db contextguessing在生成列表中对结果进行排序?CCharpAtl:是的,因此我没有显示代码。我假设Users语句中的u应该抛出一个错误构建查询将不会执行它。dbResult是执行生成的语句的可能性。一旦你调用了.ToList或Count之类的方法,或者其他类似的方法,实际的查询将用于数据源。blu:非常感谢。它使它变得非常高效/可读性CSharpatl:我想知道使用WCFTestClient测试它是否会有所不同,因为WCFTestClient会抛出一个超时异常,而不是SQL异常,因为您得到的是一个异常,但不是SqlException?我相信服务应该抛出一个SoapException而不是SqlException,即使是在服务中引发的SqlException。这取决于如何在服务上配置CustomException。