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

推荐订阅源

W
WeLiveSecurity
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
The Register - Security
The Register - Security
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
S
SegmentFault 最新的问题
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Proofpoint News Feed
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
C
Cybersecurity and Infrastructure Security Agency CISA
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
U
Unit 42
腾讯CDC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
H
Help Net Security
Recent Announcements
Recent Announcements
P
Privacy & Cybersecurity Law Blog
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 热门话题
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
博客园 - 聂微东
S
Securelist
大猫的无限游戏
大猫的无限游戏
Cloudbric
Cloudbric
Cisco Talos Blog
Cisco Talos Blog

博客园 - 李帅斌-Memory

快速导出CSV log4net使用详解 . C# 格式化字符串 web.config中特殊字符的转译 一些概念需要明确的解释一下 还原已清空的回收站 安装卸载Windows服务 Jquery 文章积累 Spring.NET学习——控制反转(基础篇) SqlServer2005导出数据到SqlServer2000步骤! - 李帅斌-Memory - 博客园 MSSQL2005:SQL Server 2005安装图解 网站优化-Gzip压缩 Asp.Net读取excel文件的时候 出错提示:外部表不是预期的格式 解决方案 js触发回车事件 - 李帅斌-Memory - 博客园 在Div中包含的元素移动时,触发div的onmouseout事件.解决方案! - 李帅斌-Memory - 博客园 VisualStudio用IE8调试时遇到的问题 JQuery+Ajax实现无刷新数据查询 Asp.net页面执行两遍Page_Load的解决方案 C#取硬盘序列号(-备忘-)
transactionscope的使用
李帅斌-Memory · 2011-01-21 · via 博客园 - 李帅斌-Memory

TransactionScope 是的.net Framework2.0版本中增加的一个新命名空间。他的用途是为数据库访问提供一个“轻量级”的事物。

使用时要添加System.Transactions.dll的引用

示例代码如下:

using(TransactionScope scope = new TransactionScope())

{

  //Query

  using(SqlConnection conn = new SqlConnection(connString))

  {

    SqlCommand cmd = new SqlCommand(sqlQuery,conn);

    conn.open();

    cmd.ExecuteNonQuery();

  }

  //Update()

  using(SqlConnection conn = new SqlConnection(connString))

  {

    SqlCommand cmd = new SqlCommand(sqlUpdate,conn);

    conn.open();

    cmd.ExecuteNonQuery();

  }

  //Insert()

  using(SqlConnection conn = new SqlConnection(connString))

  {

    SqlCommand cmd = new SqlCommand(sqlInsert,conn);

    conn.open();

    cmd.ExecuteNonQuery();

  }

  scope.Complete();

}

TransactionScope嵌套应用

如下列代码,假设 Method1 创建一个 TransactionScope,针对一个数据库执行一条命令,然后调用 Method2。Method2 创建一个自身的 TransactionScope,并针对一个数据库执行另一条命令。      

private void Method1()
{
  using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required))
  {
    using (SqlConnection cn2005= new SqlConnection())
    {
      SqlCommand cmd= new SqlCommand(updateSql1, cn2005);
      cn2005.Open();
      cmd.ExecuteNonQuery();
    }

    //调用Method2
    Method2();
    ts.Complete();
  }
}
private void Method2()
{
  using (TransactionScope ts=new TransactionScope(TransactionScopeOption.RequiresNew))
  {
    using (SqlConnection cn2005= new SqlConnection())
    {
      SqlCommand cmd= new SqlCommand(updateSql2, cn2005);
      cn2005.Open();
      cmd.ExecuteNonQuery();
    }
    ts.Complete();
  }
}