2012-12-21 17 views
1

데이터베이스에서 데이터를 가져와 GridView의 클라이언트 페이지에 표시하는 서비스를 만들고 싶습니다.하지만 데이터가 어떻게 관리 될지 문제가 있습니다. 그리드보기의 데이터 테이블을 통해 서비스에서 Plz 도와주세요.어떻게 그리드 뷰에서 WCF를 통해 서비스에서 오는 데이터를 관리 할 것인가?

서비스 페이지

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 
using System.Text; 
sing System.Data; 
using System.Data.SqlClient; 
using System.Web; 
using System.Web.Configuration; 
[ServiceContract(Namespace = "myService")] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class Service 
{ 
    static private string sqlConString 
    { 
     get { return WebConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString; } 
    } 
    SqlConnection conn = new SqlConnection(sqlConString); 

    [OperationContract] 
    public DataTable FatchJobProporties() 
    { 
     conn.Open(); 
     try 
     { 
      string selQuery = "SELECT [job_title],[job_company],[job_vacancies],[job_description] FROM [dbo].[tb_job]"; 

     SqlCommand cmd = new SqlCommand(selQuery, conn); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds, "tb_Job"); 
      DataTable dt = ds.Tables["tb_Job"]; 
      return dt; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 

     finally 
     { 
      conn.Close(); 
      conn.Dispose(); 
     } 
} 

[DataContract] 
public class JobProporties 
{ 
    string jobTitle = "my"; 
    string jobCompany = "my"; 
    string jobDec = "my"; 
    int jobVacant = 0; 

    [DataMember] 
    public string JobTitle 
    { 
     get { return jobTitle; } 
     set { jobTitle = value; } 
    } 

    [DataMember] 
    public string JobCompany 
    { 
     get { return jobCompany; } 
     set { jobCompany = value; } 
    } 

    [DataMember] 
    public int JobVacant 
    { 
     get { return jobVacant; } 
     set { jobVacant = value; } 
    } 

    [DataMember] 
    public string JobDec 
    { 
     get { return jobDec; } 
     set { jobDec = value; } 
    } 
} 

이름 "의 default.aspx"

![<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head runat="server"> 

    <title></title> 

<script> 

    function myFunc() { 

     var proxy = new myService.Service(); 

     proxy.FatchJobProporties(onSuccess, onFail, null); 


    } 


    function onSuccess(result) { 

     document.getElementById("tbDiv").innerHTML = result; 

    } 

    // This function is called if the service call fails 

    function onFail() { 

     alert("Fail to Fetch"); 
    } 

</script> 

</head> 

<body> 

    <form id="form1" runat="server"> 

    <div> 

     <asp:ScriptManager ID="ScriptManager1" runat="server"> 

      <Services> 

       <asp:ServiceReference Path="Service.svc" /> 

      </Services> 

     </asp:ScriptManager> 

     <input id="Button1" type="button" value="button" onclick="return myFunc();" /> 

     <div id="tbDiv"> 

     </div> 


    </div> 

    </form> 

</body> 

</html>][1] 

출력과 함께 페이지를 수신이 서비스 이름 "Service.svc"와 :

다음

내 코드입니다 : 전체 데이터가 한 줄로 표시됩니다. 출력을 보려면

+0

어디의 GridView 컨트롤은 무엇입니까? –

답변

1

여러 레코드를 반환하려면 반환 유형으로 List 클래스를 사용하십시오.

이 코드를 시도하십시오.

[DataContract] 
public class Customer 
{ 
    [DataMember] 
    public int CustomerID{ get; set; } } 

    [DataMember] 
    public string CustomerName{ get; set; }  
} 


public interface ICustomerService 
{ 

    [OperationContract] 
    List<Customer> GetAllCustomer(); 
} 


public class CustomerService:ICustomerService 
{ 

    List<Customer> GetAllCustomer() 
    { 
     List<Customer> customers = new List<Customer>(); 

     using(SqlConnection con = new SqlConnection("Database Connection String")) 
     { 
     con.Open();  
     using(SqlCommand cmd = new SqlCommand("Select * from Customer",con)) 
     { 
      SqlDataReader dr = cmd.ExecuteReader(); 

      while(dr.Read()) 
      { 
       Customer customer = new Customer(); 
       customer.CustomerID =Parse.Int(dr[0].ToString()); 
       customer.CustomerName =dr[1].ToString(); 
       customers.Add(customer); 
      } 
     } 
     } 
    return customers; 
    } 
} 

Enjoy !!!!

감사 CK 등록 니틴 (틴틴)

+1

재생 해 주셔서 감사합니다. 이제 기본 페이지에서 클라이언트 쪽에서 할 수있는 작업이 무엇입니까? –

1

때 u는 WCF의 서비스 참조를 추가합니다. 당신은

IList<Customer> customers = WCFServiceClientObject.GetAllCustomers() 
myDataGrid.DataSource = customers; 
MyDataGrid.DataBind(); 

감사

처럼 할 수

CK 등록 니틴 (틴틴)

관련 문제