从C#中返回的数据集填充对象

时间:2013-12-05 14:56:21

标签: c# object dataset

我有一个调用Oracle存储过程的Web服务,并且无需麻烦地获取数据集。 我还创建了一个类(下面),我想从数据集填充并返回这个而不是数据集,但我不知道怎么样,有人能给我一个启动器还是指向一个体面的例子?

感谢

namespace Models
{
[DataContract]
public class PaymentAccDetails
{
    [DataMember(Order = 1)]
    public string PaymentRef
    {
        get;
        private set;
    }

    [DataMember(Order = 2)]
    public DateTime StartDate
    {
        get;
        private set;
    }

    [DataMember(Order = 3)]
    public DateTime EndDate
    {
        get;
        private set;
    }

    [DataMember(Order = 4)]
    public string AccountType
    {
        get;
        private set;
    }

    [DataMember(Order = 5)]
    public string PaymentMethod
    {
        get;
        private set;
    }

    [DataMember(Order = 6)]
    public DateTime LastPaidDate
    {
        get;
        private set;
    }

    [DataMember(Order = 7)]
    public string Balance
    {
        get;
        private set;
    }

}

}

4 个答案:

答案 0 :(得分:4)

为类创建一个构造函数,该构造函数接受DataRow并从中构建对象:

public PaymentAccDetails (DataRow row)
{
     this.PaymentMethod = row["PaymentMethod"].ToString();
     this.AccountType = row["AccountType"].ToString();
     //etc...
}

称之为:

foreach(DataRow row in dataset.Tables[0].Rows)
{
    PaymentAccDetails acctDetails = new PaymentAccDetails(row);
    //add to a collection
}

答案 1 :(得分:3)

如果您当前正在使用DataAdapter来填充DataSet,但更喜欢强类型对象的List,那么使用DataReader可能会更快更直接。

using (DbDataReader reader = command.ExecuteReader()) {
  while (reader.Read()) {
    PaymentAccDetails pad = new PaymentAccDetails();
    pad.PaymentRef = reader.GetString(0);
    [fill other properties]
    [add to list]
  }
}

答案 2 :(得分:2)

var paymentDetails = dataSet.Tables[i].AsEnumerable()
                         .Select(r => new PaymentAccDetails
                         {
                             PaymentRef = r[n],
                             StartDate = r[o],
                             ...
                         }
                         .ToList();

其中i是表的索引,nPaymentRef字段的索引,oStartDate字段的索引等等。

答案 3 :(得分:1)

就个人而言,这就是我想要这样做的方式 - 通过这种方式,您确切地知道您从Web服务接收的内容:

public List<PaymentAccDetails> GetPaymentAccDetails()
{
   var paymentAccDetailsList = new List<PaymentAccDetails>();
   using (var conn = new SqlConnection(/*ConnectionString*/))
   { 
        conn.Open();
        using (var cmd  = new SqlCommand("YourStoredProcedure", conn))
        {
             cmd.CommandType = CommandType.StoredProcedure;
             using (var reader = cmd.ExecuteReader())
             {
                 while (reader.Read())
                 {
                     var paymentAccDetails = new PaymentAccDetails();
                     paymentAccDetails.PaymentMethod = Convert.ToString(row["PaymentMethod"]);
                     paymentAccDetails.AccountType = Convert.ToString(row["AccountType"]);
                     ... etc.
                     paymentAccDetailsList.Add(paymentAccDetails);
                 }
             }
        }
    }
    return paymentAccDetailsList;
}