简体   繁体   English

WCF服务返回DataSet

[英]WCF service returning DataSet

I have this WCF service: 我有这个WCF服务:

public DataSet GetInfo()
{
        DataTable dt = new DataTable("Tbl");
        DataSet ds = new DataSet("Set");        
        OdbcCommand OdbcCmd;
        OdbcCmd = new OdbcCommand("select * FROM Products where id = 'JBE-235'", OdbcConn);          
        OdbcConn.Open();
        dt.Load(OdbcCmd.ExecuteReader());
        ds.Tables.Add(dt);           
        OdbcConn.Close();          
        return ds;
}

But I've read that returning a DataSet from a WCF service is a bad practice, I have a desktop application and I need to fill a DataGridView with the service's result. 但我已经读过从WCF服务返回DataSet是一种不好的做法,我有一个桌面应用程序,我需要用服务的结果填充DataGridView

private void ButtonInfo_Click(object sender, EventArgs e)
{ 
     WCFService service = new WCFService();
     DataGridView1.DataSource = service.GetInfo();
     service.Close();
}

What kind of data type should I get from the WCF service to fill the DataGridView correctly? 我应该从WCF服务获取什么样的数据类型才能正确填充DataGridView

Thanks in advance. 提前致谢。

The better way is to return DTO (in your case it will be collection of DTOs). 更好的方法是返回DTO (在您的情况下,它将是DTO的集合)。

First, create DTO class that will contains expected fields from Products table. 首先,创建将包含Products表中的预期字段的DTO类。 For example: 例如:

public class Product
{
    public string Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

Next, read values from database via DataReader : 接下来,通过DataReader从数据库中读取值:

// use IEnumerable<Product> for .net <= 4.0 and IReadOnlyCollection<Product> for .net >= 4.5
public IReadOnlyCollection<Product> GetInfo()
{
    OdbcCommand command = new OdbcCommand("select * FROM Products where id = 'JBE-235'", OdbcConn);          
    OdbcConn.Open();
    var reader = command.ExecuteReader();
    var products = new List<Product>();
    while (reader.Read())
    {
        var product = new Product();
        // reader index is the column name from query
        // You can also use column index, for example reader.GetString(0)
        product.Id = (string) reader["id"];
        product.Name = (string) reader["name"];
        product.Price = (decimal) reader["price"];
        products.Add(product);
    }
    return products;
}

Note that you need to close connection after reading all data, then the good practice is to use using statement to dispose it in the end of your method automatically. 请注意,您需要在读取所有数据后关闭连接,然后好的做法是使用using语句自动将其置于方法的末尾。 The other advice is to use command Parameters : 另一个建议是使用命令参数

// use IEnumerable<Product> for .net <= 4.0 and IReadOnlyCollection<Product> for .net >= 4.5
public IReadOnlyCollection<Product> GetInfo()
{
    using(var con = GetConnection())
    {
        var cmd = new OdbcCommand("select * FROM Products where id = @Id", con);
        cmd.Parameters.AddWithValue("@Id", "JBE-235");
        con.Open();
        var reader = cmd.ExecuteReader();
        var products = new List<Product>();
        while (reader.Read())
        {
            products.Add(new Product { Id = (string) reader["id"], Name = (string) reader["name"], Price = (decimal) reader["price"] });
        }
        return products;
    }
}

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

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