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

推荐订阅源

T
Tenable Blog
P
Privacy International News Feed
L
LINUX DO - 热门话题
T
Threatpost
Latest news
Latest news
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
Cyberwarzone
Cyberwarzone
Spread Privacy
Spread Privacy
Recent Announcements
Recent Announcements
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
NISL@THU
NISL@THU
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
V
Vulnerabilities – Threatpost
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
H
Help Net Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cisco Blogs
Security Latest
Security Latest
博客园 - 叶小钗
博客园 - Franky
The Hacker News
The Hacker News
Engineering at Meta
Engineering at Meta
Scott Helme
Scott Helme
S
Securelist
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
A
About on SuperTechFans
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园_首页
B
Blog RSS Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
月光博客
月光博客
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tor Project blog
A
Arctic Wolf

博客园 - 疾行者

关于开发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