Inserting rows of database into XML/XSD class C# -


summary: used xsd.exe create class off of xml template. let's suppose there's class named human 2 attributes: name(string) , id(int). table, humans, has 2 columns named name , id. i'm trying use sqldatareader read in rows of database , create human object each row. there smarter way insert each column 1 of attributes this? else, need many 'if' statements check name of each row , correlate attribute.

xml class  public partial class human     {          private string name;          private int id;          /// <remarks/>         [system.xml.serialization.xmlattributeattribute()]         public string name         {                         {                 return this.namefield;             }             set             {                 this.namefield = value;             }         }          /// <remarks/>         [system.xml.serialization.xmlattributeattribute()]         public int id         {                         {                 return this.idfield;             }             set             {                 this.idfield = value;             }         }     } 

** after connect has been established database using sqlconnection

sqlcommand cmd = new sqlcommand("select * tablename"); sqldatareader reader = cmd.executereader(); while (reader.read()) {    //iterating through columns of specific row    (int = 0; < reader.fieldcount; i++)     {        human human1 = new human();        string currentcolvalue = reader[i].tostring();        string currentcolname = reader.getname(i);        if (currentcolname == "name" ) { human.name = currentcolvalue; }        else { human.id = (int) currentcolvalue; } //now know it's other attribute        //now insert human larger class or array    } } 

as can see, 2 column table it's relatively easy. if had table multiple columns, becomes way harder. there easier way this?

it seems nhibernate way go, learning curve appears quite steep - mind giving me sample code?

instead of using full fledge orm (object relational mapping) nhibernate , entity framework, decided use petopoco. in opinion, easier learn on nhibernate simple class file.

instead of solution taking n number of if statements n columns, took single lines of code!

ienumerable<human> humanlist = (new petapoco.database(connectionstring)) .fetch<human>("select * dbtable"); 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

android - CollapsingToolbarLayout: position the ExpandedText programmatically -

Listeners to visualise results of load test in JMeter -