score:2

Accepted answer

if you are working with sql server, ado.net + linq-to-sql is definitely the way to go.

reasons for linq:

  1. it fits naturally with your .net code, ide, and debugger
  2. you don't have to maintain your code in one place and your queries in another
  3. you aren't hard-coding sql queries and you don't have to go to the trouble of creating paramterized sql (which is really a pain in my opinion)
  4. it is super fast to write and surprisingly flexible
  5. your code will be cleaner and smaller because you don't have to have ado.net and database plumbing everywhere - linq takes care of all the hard stuff
  6. you get a nice entity designer that allows you to drag-n-drop from a sql connection

reasons not to use linq:

  1. if you have to squeeze every last ounce of performance out of a query, linq may not be the most natural choice because sql can sometimes be more flexible. but you should really consider your query carefully to see if there is an easier way to do it.
  2. some places don't like the security implications because the tables have to be public to your connected user. some companies only allow access to tables via sprocs.

for your example, you'll want to create classes (poco's) that represent your database entities. then you'll use linq to query your tables and load data into the new entity objects. i prefer to have a constructor on my domain/entity objects that transform the persistent database objects generated by linq into domain-layer objects to be used by the client.

public class person
{
    public int id { get; set; }
    public string surname { get; set; }
    public string name { get; set; }
    public short age { get; set; }

    public person()
    {
    }

    public person( persistence.person src )
    {
      this.id = src.id;
      this.surname = src.surname;
      this.name = src.name;
      this.age = src.age;
    }
}

...
public list<domain.person> loadpeople()
{
    using( var context = this.createcontext() )
    {
        var personquery = from p in context.persons
                          select new domain.person( p );

        return personquery.tolist();
    }
}

public person loadperson( int personid )
{
    using( var context = this.createcontext() )
    {
        var personquery = from p in context.persons
                          where p.id == personid
                          select new domain.person( p );

        return personquery.singleordefault();
    }
}

score:1

assuming you have the following tables:

tblusers
userid lname fname genderid
1      joe   chan  1
2      koh   wang  2
3      john  chen  1


tblgenders
genderid gender
1        male
2        female

you can use this code to get a dataset that would contain the combined information from the two tables above.

        sqlconnection con = new sqlconnection(connection string for you database);
        con.open();
        sqlcommand comm = con.createcommand();
        comm.commandtext = "select u.userid, u.fname, u.lname, u.genderid, g.gender 
                            from tblusers u, tblgenders g 
                            where u.genderid = g.genderid";
        sqldataadapter da = new sqldataadapter(comm);
        dataset ds = new dataset();
        da.fill(ds);

you can access the rows under the table by using this code da.table[0].rows;

if you are already knowledgeable with stored procedures you can also use this code:

    internal static list<roomtype> fetchroomtypelist()
    {
        list<roomtype> roomtypes = new list<roomtype>();
        sqlcommand commroomtypeselector = connectionmanager.mainconnection.createcommand();
        commroomtypeselector.commandtype = commandtype.storedprocedure;
        commroomtypeselector.commandtext = "rooms.asp_rms_roomtypelist_select";
        sqldataadapter da = new sqldataadapter(commroomtypeselector);
        dataset ds = new dataset();
        da.fill(ds);
        roomtypes = (from rt in ds.tables[0].asenumerable()
                     select new roomtype
                     {
                         roomtypeid = rt.field<int>("roomtypeid"),
                         roomtypename = rt.field<string>("roomtype"),
                         lasteditdate = rt.field<datetime>("lasteditdate"),
                         lastedituser = rt.field<string>("lastedituser"),
                         isactive = (rt.field<string>("isactive") == "active")
                     }
                    ).tolist();

        return roomtypes;
    }

for more information visit:

http://www.homeandlearn.co.uk/csharp/csharp.html


Related Query

More Query from same tag