2013-02-28 30 views
0

我已填充數據讀取器從數據庫表和我有類似如何反序列的DataReader或數據表到c#類

public class CandidateApplication 
{ 
       public string EmailID { get; set; } 
       public string Name { get; set; } 
       public string PhoneNo { get; set; } 
       public string CurrentLocation { get; set; } 
       public string PreferredWorkLocation { get; set; } 
       public int RoleApplingFor { get; set; } 
       public string CurrentJobTitle { get; set; } 
       public int EducationLevel { get; set; } 
       public decimal SalaryExpected { get; set; } 
       public string AvailableTime { get; set; } 
       public int AdvertID { get; set; } 
       public bool SignForAlert { get; set; } 
       public string CVInText { get; set; } 
       public string CVFileName { get; set; } 
       public bool IsDownloaded { get; set; } 
       public string specialization { get; set; } 
       public bool isallocated { get; set; } 
       public int id { get; set; } 
       public string AdvertAdditionalInfo { get; set; } 
} 

我可以填充在環上面的類。我們可以迭代數據讀取器並填充類,但我想知道是否有任何捷徑從數據讀取器填充類。

如果數據反序列化可能從數據讀取器到類,那麼也告訴我,如果數據讀取器中沒有數據領域中存在的幾個字段,那麼如何處理這種情況。

+1

這似乎在這裏得到解答: http://stackoverflow.com/questions/1464883/如何可以輕鬆地轉換datareader到列表 – markoo 2013-02-28 12:10:58

回答

1

您不需要使用數據讀取器,只需將數據填充到DataTable中,然後使用下面的方法創建CandidateApplication類的List。

呼叫: -

List<CandidateApplication> CandidateList = GetCandidateInformation(); 

生成的列表的方法: -

public List<CandidateApplication> GetCandidateInformation() 
     { 
      DataTable dt = new DataTable(); 

      using (OleDbConnection con = new OleDbConnection(ConfigurationManager.AppSettings["con"])) 
      { 
       using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM [TableName]", con)) 
       { 
        var adapter = new OleDbDataAdapter(); 
        adapter.SelectCommand = cmd; 

        con.Open(); 
        adapter.Fill(dt); 

        var CandApp = (from row in dt.AsEnumerable() 

        select new CandidateApplication 
        { 

        EmailID = row.Field<string>("EmailID"), 
        Name = row.Field<string>("Name"), 
        PhoneNo = row.Field<string>("PhoneNo"), 
        CurrentLocation = row.Field<string>("CurrentLocation"), 
        PreferredWorkLocation = row.Field<string>("PreferredWorkLocation"), 
        RoleApplingFor = row.Field<int>("RoleApplingFor"), 
        CurrentJobTitle = row.Field<string>("CurrentJobTitle"), 
        EducationLevel = row.Field<int>("EducationLevel "), 
        SalaryExpected = row.Field<decimal>("SalaryExpected"), 
        AvailableTime = row.Field<string>("AvailableTime"), 
        AdvertID = row.Field<int>("AdvertID"), 
        SignForAlert = row.Field<bool>("SignForAlert"), 
        CVInText = row.Field<string>("CVInText"), 
        CVFileName = row.Field<string>("CVFileName"), 
        IsDownloaded = row.Field<bool>("IsDownloaded"), 
        Specialization = row.Field<string>("Specialization"), 
        Isallocated = row.Field<bool>("Isallocated"), 
        Id = row.Field<int>("Id"), 
        AdvertAdditionalInfo = row.Field<string>("AdvertAdditionalInfo") 


        }).ToList(); 

        return CandApp; 
       } 
      } 
     } 
+0

我有一個問題,當我們提取數據並分配給像EmailID = row.Field (「EmailID」)類屬性如果電子郵件ID或任何整數或日期字段爲空,那麼如何處理...請指導我的代碼。另一個問題,我們不能把null分配給int,bool和datetime類型的變量嗎? – Thomas 2013-03-01 07:33:31

+0

你遇到過問題嗎?最新的錯誤信息? – Derek 2013-03-01 12:10:43

+0

我沒有遇到任何問題,因爲我的數據中沒有空值。所以我想知道如果在數據中有空然後會發生什麼...發生任何錯誤。如果是,那麼如何處理錯誤。 – Thomas 2013-03-01 18:27:01

2

雖然不是回答你的問題,我建議你考慮以下解決方法,它使用的SqlDataAdapter而不是數據讀取器:

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.SqlClient; 
using System.IO; 
using System.Xml.Serialization; 

class Program 
{ 
    static void Main(string[] args) 
    { 

     var cs = "YourConnectionString"; 
     var xml = ""; 
     using (var con = new SqlConnection(cs)) 
     using (var c = new SqlCommand("SELECT * FROM CandidateApplication", con)) 
     { 
      con.Open(); 
      using (var adapter = new SqlDataAdapter(c)) 
      { 
       var ds = new DataSet("CandidateApplications"); 
       ds.Tables.Add("CandidateApplication"); 
       adapter.Fill(ds, ds.Tables[0].TableName); 
       xml = ds.GetXml(); 
      } 
     } 

     // We need to specify the root element 
     var rootAttribute = new XmlRootAttribute(); 

     // The class to use as the XML root element (should match the name of 
     // the DataTable in the DataSet above) 
     rootAttribute.ElementName = "CandidateApplications"; 

     // Initializes a new instance of the XmlSerializer class that can 
     // serialize objects of the specified type into XML documents, and 
     // deserialize an XML document into object of the specified type. 
     // It also specifies the class to use as the XML root element. 
     // I chose List<CandidateApplication> as the type because I find it 
     // easier to work with (but CandidateApplication[] will also work) 
     var xs = new XmlSerializer(typeof(List<CandidateApplication>), rootAttribute); 

     // Deserialize the XML document contained by the specified TextReader, 
     // in our case, a StringReader instance constructed with xml as a parameter. 
     List<CandidateApplication> results = xs.Deserialize(new StringReader(xml)); 
    } 
} 

對於缺少在檢索到的數據的特性,你可以聲明私有字段的默認值:

string _advertAdditionalInfo = "default"; 
public string AdvertAdditionalInfo 
{ 
    get 
    { 
     return _advertAdditionalInfo; 
    } 
    set 
    { 
     _advertAdditionalInfo = value; 
    } 
} 

如果你想執行檢索到的數據不會在一個特定的屬性填寫,使用:

[XmlIgnoreAttribute] 
public string AdvertAdditionalInfo { get; set; } 
+0

你會大聲說明主要方法的最後5行。只要告訴我每條線在細節上做了什麼。謝謝 – Thomas 2013-03-01 07:36:04

+0

如何在你的情況下處理null?假設當你從數據庫中檢索數據,那麼可能會有空值,並且將在數據集中分配空值。所以當你將數據集反序列化到CandidateApplication時,將如何處理空值?或者當在CandidateApplication實例反序列化時找到null時如何爲任何屬性賦值?我的兩個問題非常重要。請儘可能詳細回答。謝謝 – Thomas 2013-03-01 07:40:14

+0

我已經用一些內嵌評論更新了我的答案。 'rootAttribute.IsNullable = true;'不是真的需要,所以我刪除了這一行。 – 2013-03-01 07:53:27