惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
月光博客
月光博客
AI
AI
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CXSECURITY Database RSS Feed - CXSecurity.com
WordPress大学
WordPress大学
GbyAI
GbyAI
L
Lohrmann on Cybersecurity
O
OpenAI News
Schneier on Security
Schneier on Security
P
Palo Alto Networks Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Troy Hunt's Blog
V2EX - 技术
V2EX - 技术
W
WeLiveSecurity
L
LINUX DO - 最新话题
人人都是产品经理
人人都是产品经理
S
Security Affairs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
Arctic Wolf
Recorded Future
Recorded Future
Microsoft Security Blog
Microsoft Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
GRAHAM CLULEY
N
Netflix TechBlog - Medium
TaoSecurity Blog
TaoSecurity Blog
C
Check Point Blog
Scott Helme
Scott Helme
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
PCI Perspectives
PCI Perspectives
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Vercel News
Vercel News
The Hacker News
The Hacker News
Y
Y Combinator Blog
Latest news
Latest news
SecWiki News
SecWiki News
Hugging Face - Blog
Hugging Face - Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cisco Blogs
博客园_首页
H
Hackread – Cybersecurity News, Data Breaches, AI and More
宝玉的分享
宝玉的分享
L
LINUX DO - 热门话题

博客园 - 疾行者

关于开发ActiveX 控件的安装问题 [转]安卓巴士Android开发神贴整理 Ruby On Rails 安装手记 偶然发现5年前的编程实习日记,特与博客园同仁分享一下 实现客户端的DataSet - 疾行者 - 博客园 AutoIt应用技巧示例[转] - 疾行者 - 博客园 [导入]Linq学习笔记(1.7)——First、ElementAt、Any、all [导入]Linq学习笔记(1.8)——Count、Sum、Min、Max、Average [导入]Linq学习笔记(2.1)——初识 DLinq [导入]Linq学习笔记(2.2)——深入DLinq查询 NHibernate 多主键设置 - 疾行者 - 博客园 c#的一些技巧 - 疾行者 - 博客园 人的一生,到底在追求甚么?... XP 风格的可拖动列、可排序、可改变宽度的DataGrid的例子 webservice结合dthml的简单例子(二,dhtml) 文件的上传下载 asp.net访问组件失败,请在web.,config中加 数字转化成大写类 javascript小技巧&&JavaScript[对象.属性]集锦 [转载了多篇]
[导入]Linq学习笔记(2.3)——DLinq高级操作
疾行者 · 2008-01-04 · via 博客园 - 疾行者

     前面我们学习了使用Dlinq,从面向对象的角度操作数据库的基本一些知识,今天我学习了Dlinq直接执行SQL命令可查询,执行存储过程,事务处理的一些知识,下面是我联系时的一些Demo。
     在这次学习前先向大家将两个Dlinq的知识:
     1:使用工具生成实体。
     前面学习的时候大家可能感觉最麻烦的是根据数据库写映射实体,vs专门提供了工具(C:\Program Files\Microsoft Visual Studio 9.0\SDK\v3.5\Bin\SqlMetal.exe)方便我们自动生成实体,
     SqlMetal /server:.\SQLExpress /database:Northwind /pluralize /namespace:Arcadia /code:d:\Northwind.cs
  大家可以在d:目录下找到Northwind.cs,将其拷贝到您的工程下,下面我们的代码就是在此文件下操作。

     2:显示Linq语句编译后的Sql语句
     方法1:用DataContext的Log属性。db.Log = Response.Output;
     方法2:用GetQueryText / GetChangeText。GetQueryText用于查询时使用,GetChangeText用于更新或删除。

var products = db.Products.Where(p => p.OrderDetails.Count > 50);
Response.Write(db.GetQueryText(products));

ExecuteQuery

    Northwind db = new Northwind("data source=.\\SQLEXPRESS;Integrated Security=SSPI;");
    var products 
= db.ExecuteQuery<Product>("select * from Products where ProductID<10");
    products.ToList().ForEach(p 
=> Response.Write(p.ProductName + "<br>"));

ExecuteCommand

   Northwind db = new Northwind("data source=.\\SQLEXPRESS;Integrated Security=SSPI;");
    db.ExecuteCommand(
"update [Products] set [ProductName] = {0} where [ProductID]={1}""Young's book"1);

ExecuteCommand用户更新,删除等操作, ExecuteQuery用于查询操作,所以在执行存储过程时要根据情况使用不同的语句。

        Northwind db = new Northwind("data source=.\\SQLEXPRESS;Integrated Security=SSPI;");
        var orders 
= db.ExecuteQuery<Order>("exec CustOrdersOrders @CustomerID={0}""ALFKI");
        orders.ToList().ForEach(o 
=> Response.Write(o.OrderID + "<br>"));

Transactions

Northwind db = new Northwind("data source=.\\SQLEXPRESS;Integrated Security=SSPI;");

        
if (db.Connection != null) db.Connection.Open();
        db.Transaction 
= db.Connection.BeginTransaction();
        IEnumerator
<OrderDetail> orders = db.OrderDetails.Where(o => o.ProductID == 1).GetEnumerator();        
        
while (orders.MoveNext())
        
{
            OrderDetail od 
= orders.Current;
            od.UnitPrice 
= 200;
        }


        var prod 
= db.Products.Single(p => p.ProductID == 1);
        db.Products.Remove(prod);
        
try{
            db.SubmitChanges();
            db.Transaction.Commit();
        }

        
catch{
            db.Transaction.Rollback();
            
throw;
        }

        
finally{
            db.Transaction 
= null;
        }

    官方的文档介绍Dinq的Transaction这样使用的,不过我有一点不明白,即使不使用Transaction,我测试SubmitChanges()本身就有类似Transaction的功能,如果一系列操作中有一个操作失败,这一系列操作都不会成功,请高手分析一下使用Transaction和直接SubmitChanges()的区别。
     上面这些是用来弥补Dlinq不好用对象处理的复杂数据库操作情景,上面的例子很简单,为了方便理解其使用方法。

Young.J 2007-06-17 19:27 发表评论

文章来源:http://www.cnblogs.com/young18/archive/2007/06/17/786651.html