Data Access FAQ (二)
这里是ASP.NET Data Access FAQ的第二部分: LINQ How can I implement a transaction in LINQ? A: You can use TransactionScope class in LINQ to implement a transaction. Its a new function in .NET Framework 2.0 to provide an implicit way to impl
这里是ASP.NET Data Access FAQ的第二部分:
LINQ
How can I implement a transaction in LINQ?
A: You can use TransactionScope class in LINQ to implement a transaction. It’s a new function in .NET Framework 2.0 to provide an implicit way to implement a transaction. You can use it in LINQ as shown below:
using (TransactionScope scope = new TransactionScope())
{
try
{
……….
ctx.SubmitChanges();
……….
ctx.SubmitChanges();
}
catch (Exception ex)
{
Response.Write("Error happens, Transaction class will automaticlly roll back!");
}
scope.Complete();
}
You need to reference the System.Transactions assembly and add the namespace ‘System.Transactions’. Also, you need to make sure the windows service-“Distributed Transaction Coordinator Service” is running.
Related link:
http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx
How can I use left join in LINQ.
A: You can use the keywords “join” and “into” to implement left join in LINQ. Please take a look at following example:
var sel = from u in
join p inon u.TagID equals p.TagID into UP
from p in UP.DefaultIfEmpty()
select new
{
UT = u.TagID,
UT1 = u.Text,
UT2 = p.Info
};
What’s the difference between List
A: You can return LINQ query result as type of both List
List
// Return List
ListUser> users = res.ToListUser
推荐阅读
-
2011年计算机等考二级Access考前模拟题(3)
-
在ASP.NET 2.0中操作数据之五十三:在Data Web控件显示二进制数据
-
科捷X-DATA重磅登陆第二届数字中国建设峰会
-
微软发布的Data Access Application Block的使用代码
-
MAVEN报错Cannot access alimaven / idea data注解不好使
-
Windows 的 Oracle Data Access Components (ODAC)
-
System.data.sqlclient.sqlexception:将截断字符串或二进制数据终止
-
Spring Data 系列之JPA(二)
-
Item 66: Synchronize access to shared mutable data
-
荐 Oracle Data Guard(二)DG服务&故障恢复