1

我想创建一个从数据库中获取数据并在 GridView 的客户端页面上显示它们的服务,但是我有一个问题,我将如何通过 Grid View 中的数据表管理来自服务的数据,请帮助我。

以下是我的代码:

名称为“Service.svc”的服务页面

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]

输出:整个数据在一行中;输出见图片

4

2 回答 2

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;
  }
}

享受!!!!

谢谢CK Nitin (TinTin)

于 2012-12-21T05:15:55.630 回答
1

什么时候添加 WCF 的服务引用。你可以这样做

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

谢谢

CK 尼丁 (TinTin)

于 2012-12-21T07:10:40.820 回答