2010-11-22 83 views
3

存在作爲分隔數據的數據庫中的表格數據。我不知道,但我知道,但這就是系統設計的方式。我需要將數據提取出來並解析成某種結構。System.Data.DataTable的輕量級替換

在過去,我會使用一個DataTable並稱它爲好。但是,現在只是覺得很髒。在我的數據模型中存儲表格數據的更好,更高效,更輕量級的結構是什麼?

+0

有兩種選擇,我想到的是IList的>和IList的>。我們只限於六欄,所以我不需要超過六欄。 – Josh 2010-11-22 17:06:45

回答

3

嗯,只需創建一個類,其中包含數據庫的所有列作爲屬性。然後實例化ICollection(List,Hashset等)的實現並填充它(例如使用LINQ)。

public class Customer 
    { 
    public int Id { get; set;} 
    public string Name { get; set; } 

    public Customer(int id, string name) 
    { 
     this.Id = id; 
     this.Name = name; 
    } 
    } 

而且做這樣的事情:

List<Customer> customers = new List<Customer>(); 
using (DbDataReader reader = // instantiate reader) 
{ 
    while (reader.Read()) 
    { 
     Customer customer = new Customer(reader.GetInt32(0), reader.GetString(1)); 
     customers.Add(customer); 
    } 
} 

如果您正在訪問存儲在數據庫中的數據你可能想看看LinqToSql或LinqToEntities。

+0

我使用流利的nhibernate。此表格數據作爲分隔字符串存儲在數據庫的單個字段中。我需要將它從字符串中解析出來並放入一個對象中。 – Josh 2010-11-22 17:02:27

+2

然後只是實現你的算法來提取數據,用你需要的屬性創建一個類並填入一個ICollection ! :) – 2010-11-22 17:04:28