I’m currently doing the following to use typed datasets in vs2008:
Right click on ‘app_code’ add new dataset, name it tableDS.
Open tableDS, right click, add ‘table adapter’
In the wizard, choose a pre defined connection string, ‘use SQL statements’
select * from tablename and next + next to finish. (I generate one table adapter for each table in my DB)
In my code I do the following to get a row of data when I only need one:
cpcDS.tbl_cpcRow tr = (cpcDS.tbl_cpcRow)(new cpcDSTableAdapters.tbl_cpcTableAdapter()).GetData().Select(‘cpcID = ‘ + cpcID)[0];
I believe this will get the entire table from the database and to the filtering in dotnet (ie not optimal), is there any way I can get the tableadapter to filer the result set on the database instead (IE what I want to is send select * from tbl_cpc where cpcID = 1 to the database)
And as a side note, I think this is a fairly ok design pattern for getting data from a database in vs2008. It’s fairly easy to code with, read and mantain. But I would like to know it there are any other design patterns that is better out there? I use the datasets for read/update/insert and delete.
A bit of a shift, but you ask about different patterns – how about LINQ? Since you are using VS2008, it is possible (although not guaranteed) that you might also be able to use .NET 3.5.
A LINQ-to-SQL data-context provides much more managed access to data (filtered, etc). Is this an option? I’m not sure I’d go ‘Entity Framework’ at the moment, though (see here).
Edit per request:
to get a row from the data-context, you simply need to specify the ‘predicate’ – in this case, a primary key match:
The use of Single above is deliberate – this particular usage [Single(predicate)] allows the data-context to make full use of local in-memory data – i.e. if the predicate is just on the primary key columns, it might not have to touch the database at all if the data-context has already seen that record.
However, LINQ is very flexible; you can also use ‘query syntax’ – for example, a slightly different (list) query:
etc