在VS 2008中使用类型数据集编程模式 [英] Programming pattern using typed datasets in VS 2008
问题描述
我目前正在使用vs2008中的类型数据集:
右键单击app_code添加新数据集,将其命名为tableDS。
打开tableDS,右键单击,添加table adapter
在向导中,选择一个预定义的连接字符串,use SQL语句
select * from tablename和next + next完成。 (我为我的数据库中的每个表生成一个表适配器)
在我的代码中,我只需要一个数据就可以获取一行数据:
cpcDS.tbl_cpcRow tr =(cpcDS.tbl_cpcRow)(new cpcDSTableAdapters.tbl_cpcTableAdapter())。GetData()。Select(cpcID =+ cpcID)[0] p>
我相信这将从数据库中获取整个表格,并在dotnet中进行过滤(即不是最佳的),有没有办法让tableadapter文件化结果集在数据库上(IE我想要发送select *从tbl_cpc其中cpcID = 1到数据库)
作为一个附注,我认为这是一个相当好的设计模式,用于从vs2008中的数据库获取数据。它是相当容易的代码,阅读和mantain。但是我想知道有没有更好的其他设计模式呢?我使用数据集进行读取/更新/插入和删除。
有一点转变,但是你询问不同的模式 - LINQ怎么样?由于您使用的是VS2008,可能(尽管不能保证)您也可以使用.NET 3.5。
LINQ to SQL数据上下文提供更多的管理访问数据(过滤等)。这是一个选项吗?我不知道现在我会去实体框架( see here )。
根据请求编辑
从数据上下文中获取一行,您只需要指定谓词 - 在这种情况下,主键匹配:
int id = ... //我们要查找
的主键(var ctx = new MydataContext()){
SomeType record = ctx.SomeTable.Single(x = > x.SomeColumn == id);
// ... etc
// ctx.SubmitChanges(); //提交任何更新
}
使用单一上述是故意的 - 使用[单(谓词)]允许数据上下文充分利用本地内存数据 - 即如果谓词仅在主键列上,则如果数据上下文可能不需要触摸数据库已经看到了这个记录。
然而,LINQ非常灵活;您还可以使用查询语法 - 例如稍微不同(列表)查询:
var myOrders = from row in ctx.Orders
其中row.CustomerID = id&&& row.IsActive
orderby row.OrderDate
select row;
等
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:
int id = ... // the primary key we want to look for
using(var ctx = new MydataContext()) {
SomeType record = ctx.SomeTable.Single(x => x.SomeColumn == id);
//... etc
// ctx.SubmitChanges(); // to commit any updates
}
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:
var myOrders = from row in ctx.Orders
where row.CustomerID = id && row.IsActive
orderby row.OrderDate
select row;
etc
这篇关于在VS 2008中使用类型数据集编程模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!