2017-12-11 2 views
1

Angular JS Application에서 단일 열 레코드를 검색하려고합니다. 입력 된 텍스트에 정확한 텍스트를 입력하고 있지만 문제는 데이터 판독기 개체가 항상 false를 반환한다는 것입니다.ADO.NET Reader는 항상 Wcf 서비스에서 False를 반환합니다.

다음은 인터페이스입니다.

[OperationContract를]

[WebInvoke(Method = "GET", 
RequestFormat = WebMessageFormat.Json, 
ResponseFormat = WebMessageFormat.Json, 
UriTemplate = "/GetCustomers/{Account_Holder_Last_Name}")] 
string GetCustomers(string Account_Holder_Last_Name); 

여기 구현이다.

public string GetCustomers(string Account_Holder_Last_Name) 
     { 

      List<object> customers = new List<object>(); 
      string sql = "SELECT * FROM Current_Account_Holder_Details WHERE Account_Holder_Last_Name [email protected]_Holder_Last_Name"; 
      using (SqlConnection conn = new SqlConnection()) 
      { 
       conn.ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; 
       using (SqlCommand cmd = new SqlCommand(sql)) 
       { 
        cmd.Parameters.AddWithValue("@Account_Holder_Last_Name", Account_Holder_Last_Name); 
        cmd.Connection = conn; 
        conn.Open(); 
        using (SqlDataReader sdr = cmd.ExecuteReader()) 
        { 
         if (sdr.HasRows)**//Always returns false on this line** 
         { 
          while (sdr.Read()) 
          { 

           customers.Add(new 
           { 
            Tittle = sdr["Tittle"], 
            Account_Holder_First_Name = sdr["Account_Holder_First_Name"], 
            Account_Holder_Last_Name = sdr["Account_Holder_Last_Name"], 
            Account_Holder_DOB = sdr["Account_Holder_DOB"], 
            Account_Holder_House_No = sdr["Account_Holder_House_No"], 
            Account_Holder_Street_Name = sdr["Account_Holder_Street_Name"], 
            Account_Holder_Post_Code = sdr["Account_Holder_Post_Code"], 

            Account_Holder_Occupation = sdr["Account_Holder_Occupation"], 
            Account_Number = sdr["Account_Number"] 



           }); 
          } 

         } 

        } 
        conn.Close(); 
       } 

       return (new JavaScriptSerializer().Serialize(customers)); 
      } 

     } 
다음

각도 JS 코드입니다 .. 여기

@{ 
    Layout = null; 
} 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
</head> 
<body> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script> 
    <script type="text/javascript"> 
     var app = angular.module('MyApp', []) 
     app.controller('MyController', function ($scope, $http, $window) { 
      $scope.IsVisible = false; 
      $scope.Search = function() { 
       var post = $http({ 
        method: "GET", 
        url: "http://localhost:52098/HalifaxIISService.svc/GetCustomers/{Account_Holder_Last_Name}", 
        dataType: 'json', 
        data: { Account_Holder_Last_Name: $scope.Account_Holder_Last_Name }, 

        headers: { 
         'Accept': 'application/json, text/javascript, */*; q=0.01', 
         'Content-Type': 'application/json; charset=utf-8' 
        } 
       }); 

       post.success(function (data, status) { 
        $scope.Customers = eval(data.d); 
        $scope.IsVisible = true; 
       }); 

       post.error(function (data, status) { 
        $window.alert(data.Message); 
       }); 
      } 
     }); 
    </script> 
    <div ng-app="MyApp" ng-controller="MyController"> 
     Name: 
     <input type="text" ng-model="Account_Holder_Last_Name" /> 
     <input type="button" value="Submit" ng-click="Search(Account_Holder_Last_Name)" /> 
     <hr /> 
     <table cellpadding="0" cellspacing="0" ng-show="IsVisible"> 
      <tr style="height: 30px; background-color: skyblue; color: maroon;"> 
       <th> Tittle</th> 
       <th>First Name</th> 
       <th> Last Name</th> 
       <th> DOB </th> 
       <th> House No</th> 
       <th> Street Name</th> 
       <th>Post Code</th> 
       <th> Occupation</th> 
       <th>Account Number</th> 


      </tr> 
      <tbody ng-repeat="m in Customers"> 
       <tr> 
        <td>{{m.Tittle}}</td> 
        <td>{{m.Account_Holder_First_Name}}</td> 
        <td>{{m.Account_Holder_Last_Name}}</td> 

        <td>{{m.Account_Holder_DOB}}</td> 
        <td>{{m.Account_Holder_House_No}}</td> 
        <td>{{m.Account_Holder_Street_Name}}</td> 
        <td>{{m.Account_Holder_Post_Code}}</td> 

        <td>{{m.Account_Holder_Occupation}}</td> 
        <td>{{m.Account_Number}}</td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
</body> 
</html> 

는 SQL 스크립트입니다.

CREATE TABLE [dbo].[Current_Account_Holder_Details](
    [Account_Holder_Id] [int] IDENTITY(1,1) NOT NULL, 
    [Tittle] [nvarchar](50) NOT NULL, 
    [Account_Holder_First_Name] [nvarchar](50) NOT NULL, 
    [Account_Holder_Last_Name] [nvarchar](50) NOT NULL, 
    [Account_Holder_DOB] [nvarchar](50) NULL, 
    [Account_Holder_House_No] [nvarchar](50) NOT NULL, 
    [Account_Holder_Street_Name] [nvarchar](50) NOT NULL, 
    [Account_Holder_Post_Code] [nvarchar](50) NOT NULL, 
    [Account_Holder_Occupation] [nvarchar](50) NOT NULL, 
    [Account_Number] [int] NULL, 
) 

여기는 데이터베이스 레코드입니다. enter image description here

다음은 VS2015의 스크린 샷입니다. enter image description here 다음은 응용 프로그램을 실행할 때의 결과입니다. enter image description here

+0

.HasRows를 제거하면 어떻게됩니까? Sql LocalDB를 사용하고 있습니까? 어쩌면 당신이 생각하는 곳을 지적하지 않았을까요? – pmcilreavy

답변

1
url: "http://localhost:52098/HalifaxIISService.svc/GetCustomers/{Account_Holder_Last_Name}", 
dataType: 'json', 
data: { Account_Holder_Last_Name: $scope.Account_Holder_Last_Name }, 

아마도해야합니다 :

url: "http://localhost:52098/HalifaxIISService.svc/GetCustomers/" + encodeURIComponent($scope.Account_Holder_Last_Name), 
dataType: 'json' 

Account_Holder_Last_Name 올바르게 서버 측 설정되어 있는지 확인합니다.

+0

정말 감사합니다. 시간 내 주셔서 대단히 감사합니다. @mjwills – Mohammad

관련 문제