简体   繁体   English

如何从WCF服务将表数据发送到客户端

[英]How to send table data to client from WCF service

public DataTable Get_table_data()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Id");
        dt.Columns.Add("user_name");
        dt.Columns.Add("user_email");
        dt.Columns.Add("status");
        dt.Columns.Add("Role");
        DataRow data_table_new_row = null;
        var rows = (from s in db.logins
                  where s.status == 1
                  select s);

        foreach (var r in rows)
        {
            data_table_new_row= dt.NewRow();
            dt.Rows.Add(r.Id,r.user_name,r.user_email,r.status,r.Role);
        }
        int c = dt.Rows.Count;
        return dt;
    }

i trying to make WCF application. 我试图制作WCF应用程序。 i want to get all table data from table and return data to client application.i have converted linq data to tabledata. 我想从表中获取所有表数据并将数据返回到客户端application.i已将linq数据转换为tabledata。 but it gives me error that wcf does not support for this type of return type. 但它给了我错误,wcf不支持这种类型的返回类型。

You can return a DataSet to your client, in which case you will just need to update your method return type, add the DataTable to the DataSet, and return it to the client. 您可以将DataSet返回给客户端,在这种情况下,您只需要更新方法返回类型,将DataTable添加到DataSet,然后将其返回给客户端。

However, DataSets are not the best option for data transfer in WCF. 但是,DataSet不是WCF中数据传输的最佳选择。 Instead, try creating your own custom data transfer objects (DTO), hydrate them from your LINQ query, and return the DTOs to your caller. 相反,尝试创建自己的自定义数据传输对象(DTO),从LINQ查询中对其进行水合,然后将DTO返回给调用者。

You can find out more about WCF DataContracts here: 您可以在此处找到有关WCF DataContracts的更多信息:

http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx

Here is a hypothetical User DTO: 这是一个假设的用户DTO:

[DataContract]
public class User
{
   [DataMember]
   public int Id {get;set;}
   [DataMember]
   public string UserName {get;set;}
   [DataMember]
   public string Password {get;set;}
}

I also suggest using the Request/Response pattern with WCF, in which case you will also need a Response DTO with a list of your User DTOs: 我还建议在WCF中使用请求/响应模式 ,在这种情况下,您还需要一个响应DTO以及您的用户DTO列表:

[DataContract]
public class Response
{
   [DataMember]
   public bool Success {get;set;}
   [DataMember]   
   public string Message {get;set;}
   [DataMember]
   public List<User> Users {get;set;}
}

It is generally not recommended to return a DataTable in WCF. 通常不建议在WCF中返回DataTable It is very large and bulky to send over the wire. 通过电线发送它是非常大和笨重的。 It is not the best design either. 它也不是最好的设计。

Instead create your own class and send a List of those objects to the client. 而是创建自己的类并将这些对象的List发送到客户端。

Define your class using DataContractAttribute and DataMemberAttribute : 使用DataContractAttributeDataMemberAttribute定义您的类:

[DataContract]
public class User
{
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string UserName { get; set; }
    [DataMember]
    public string UserEmail { get; set; }
    [DataMember]
    public int Status { get; set; }
    [DataMember]
    public string Role { get; set; }
}

Then in your foreach loop just populate the List<User> collection and return that instead. 然后在你的foreach循环中填充List<User>集合并返回它。

var listOfUser = new List<User>();
foreach (var r in rows)
{
    var user = new User();
    user.Id = r.Id;
    user.UserName = r.user_name;
    user.UserEmail = r.user_email;
    user.Status = r.status; 
    user.Role = r.Role;
    listOfUser.Add(user);
}
return listOfUser;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM