首页 > 编程语言 >C# 事务实现代码

C# 事务实现代码

时间:2023-09-29 16:12:32浏览次数:35  
标签:事务 Parameters C# 代码 cmd amount cnn trans productID

 //简单事务实现代码

static void Test()

{
DataTable dt = new DataTable();
SqlConnection cnn = new SqlConnection("连接字符串");
SqlCommand cmd = new SqlCommand();
cmd.Connection = cnn;
cnn.Open();
SqlTransaction trans = cnn.BeginTransaction(); //开启事务
try
{
foreach (DataRow dr in dt.Rows)
{
cmd.CommandText = "update Product set amount = @amount where productID = @productID";
cmd.Parameters.Add("@amount", SqlDbType.Int);
cmd.Parameters["@amount"].Value = Convert.ToInt32(dr["amount"]);
cmd.Parameters.Add("@productID", SqlDbType.VarChar);
cmd.Parameters["@productID"].Value = dr["productID"].ToString();
cmd.ExecuteNonQuery();
}
trans.Commit(); //成功提交
}
catch (Exception)
{
trans.Rollback(); //出错事务回滚
}
finally
{
cnn.Close();
trans.Dispose();
cnn.Dispose();
}
}

 

标签:事务,Parameters,C#,代码,cmd,amount,cnn,trans,productID
From: https://www.cnblogs.com/KevinSteven/p/17737053.html

相关文章

  • 抽象CurrentUser适配Http和Job场景
    前言获取当前请求用户的基础信息是很常见的,诸如当前用户Id,角色,有无访问权限等。通常我们可以直接使用HttpContext.User来拿到当前经过认证后的请求人信息。但是这样对于分层应用不太友好,需要安装AspNetCore.Http.Abstractions的包,这样对于这层(非Web层)来讲也有所侵入了。Curren......
  • 【题解】[CQOI2008] 传感器网络
    题意给定一张有向无环图,从中选出一棵有根树(节点编号为\(0\simn\),树根为\(n\)),使得除根节点外所有节点的出度的最大值最小。除根节点外,依次输出每个节点的父亲,并要求字典序最小。(\(1\len\le50\))*注意:由于个人习惯,这里将节点编号重编为\(1\simn+1\),根节点即为\(n+1\)......
  • Intel & AMD CPU命名规则
    IntelIntel命名规律官方文档AMDAMD命名规律官方文档......
  • [ABC286F]
    [ABC286F]GuessTheNumber2题意转换:有一个数\(n\),你不知道是多少。让你构造一个\(m\)个点(\(1\lem\le110\)),且每条边有且仅有一条出边的图。告诉你每个点沿着边走\(n\)次后到达的点,求\(n\)。思路:可以发现当\(n\)很大时,每个点最终都到达了一个环内。由于\(n\)很......
  • Rachel美式音标学习
    音标表vowel:元音;母音;diphthong:双元音;consonant:辅音,子音;辅音字母;元音UH[ʌ]发音技巧嘴型:Thisisaveryrelaxedsound.Thejawdrops.Themouthsremainnatural.舌头:Thetongueisrelaxed.Thebackpressdownjustalittlebit,andthetipisforw......
  • 【代码片段】makefile 中通过 shell 函数执行 sed
    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!cnblogs博客zhihuGithub公众号:一本正经的瞎扯先上代码:(在macos上调试通过)#defineashellfunctiontosetdebugmodetoreleasemode#whenosismacbook,usegsedasseddefinefunction_sed_set_rel......
  • Go - Change What Is Being Logged by the Standard Logger
    Problem: Youwanttochangewhatthestandardloggerlogs.Solution: UsetheSetFlagsfunctiontosetflagsandaddfieldstoeachlogline. Thedefaultbehaviorofthestandardloggeraddsthedateandtimefieldstoeachlineofthelog. Thelogpac......
  • ABC211D Number of Shortest paths
    分析一道显然的最短路,用dijkstra算法。计算最短路的同时,保存最短路个数,如果与当前最短路相同,最短路个数相加,否则到这个节点的最短路个数为上一个节点的最短路个数。AcceptedCode#include<bits/stdc++.h>usingnamespacestd;constintN=2e5+5;constintmod=1e9......
  • Go - Inspecting Errors
    Problem: Youwanttocheckforspecificerrorsorspecifictypesoferrors.Solution: Usetheerrors.Isanderrors.Asfunctions.Theerrors.Isfunctioncomparesanerrortoavalueandtheerrors.Asfunctionchecksifanerrorisofaspecifictype. Us......
  • 9.29 《代码大全2》阅读笔记
    《代码大全2》是一本非常经典的软件开发书籍。在书中,强调了比较优秀的代码结构和命名规范的重要性。书中注释的部分帮助我理解怎么去编写有意义的注释,合适的注释可以提供代码理解上的便利,但是过多或者无关的注释会干扰代码的可读性。还有书中关于代码复用和模块化的内容帮助学习......