进来对事物作一下总结,常用代码如下:
4 |
id int identity(1,1) not null primary key,
|
5 |
CurrentMoney int not null check(CurrentMoney >1),
|
6 |
CurrentName nvarchar(10) |
下面就是事物的操作:
04 |
update bank1 set CurrentMoney = CurrentMoney -200 where CurrentName = 'zs'
|
05 |
set @sum = @@error +@sum
|
06 |
update bank1 set CurrentMoney = CurrentMoney+200 where CurrentName ='ls'
|
07 |
set @sum =@@error +@sum
|
下面是利用存储过程操作事物
03 |
@fromName nvarchar(10), |
05 |
@msg nvarchar(10) output |
10 |
update bank1 set CurrentMoney =CurrentMoney -@money where CurrentName =@fromName
|
11 |
set @errsum=@errsum+@@error
|
12 |
update bank1 set CurrentMoney =CurrentMoney +@money where CurrentName = @toName
|
13 |
set @errsum =@errsum +@@error
|
26 |
declare @a nvarchar(10)
|
27 |
exec Proc_Tran 10,'ls','zs' ,@msg =@a output
|
ADO.NET的方面操作

代码
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True");
if(con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("Update bank1 set CurrentMoney = CurrentMoney - 200 where CurrentName ='"+this.txtFromName.Text.Trim()+"'",con);
SqlCommand cmd1 = new SqlCommand("update bank1 set CurrentMoney = CurrentMoney + 200 where CurrentName='"+this.txtToName.Trim()+"'",con);
SqlTransaction tran = con.BeginTransaction();//调用SqlConnection对象的BeginTransaction方法来实例化SqlTransaction对象
try
{
cmd.ExcuteNonQuery();
cmd1.ExcuteNonQuery();
tran.commit();
}
catch(SqlException ex)
{
tran.RollBack();
}
http://www.cnblogs.com/hfliyi/archive/2010/12/11/hftarena.html
进来对事物作一下总结,常用代码如下:
4 |
id int identity(1,1) not null primary key,
|
5 |
CurrentMoney int not null check(CurrentMoney >1),
|
6 |
CurrentName nvarchar(10) |
下面就是事物的操作:
04 |
update bank1 set CurrentMoney = CurrentMoney -200 where CurrentName = 'zs'
|
05 |
set @sum = @@error +@sum
|
06 |
update bank1 set CurrentMoney = CurrentMoney+200 where CurrentName ='ls'
|
07 |
set @sum =@@error +@sum
|
下面是利用存储过程操作事物
03 |
@fromName nvarchar(10), |
05 |
@msg nvarchar(10) output |
10 |
update bank1 set CurrentMoney =CurrentMoney -@money where CurrentName =@fromName
|
11 |
set @errsum=@errsum+@@error
|
12 |
update bank1 set CurrentMoney =CurrentMoney +@money where CurrentName = @toName
|
13 |
set @errsum =@errsum +@@error
|
26 |
declare @a nvarchar(10)
|
27 |
exec Proc_Tran 10,'ls','zs' ,@msg =@a output
|
ADO.NET的方面操作

代码
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True");
if(con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("Update bank1 set CurrentMoney = CurrentMoney - 200 where CurrentName ='"+this.txtFromName.Text.Trim()+"'",con);
SqlCommand cmd1 = new SqlCommand("update bank1 set CurrentMoney = CurrentMoney + 200 where CurrentName='"+this.txtToName.Trim()+"'",con);
SqlTransaction tran = con.BeginTransaction();//调用SqlConnection对象的BeginTransaction方法来实例化SqlTransaction对象
try
{
cmd.ExcuteNonQuery();
cmd1.ExcuteNonQuery();
tran.commit();
}
catch(SqlException ex)
{
tran.RollBack();
}
相关文章:
-
2021-07-21
-
2022-12-23
-
2022-12-23
-
2022-12-23
-
2022-01-10
-
2021-12-14
-
2022-02-10