C# 在数据库中更新数据有错

来源:百度知道 编辑:UC知道 时间:2024/05/20 06:28:27
我是想在在帐户表中的的金额更新后,在读取赋给资金变更表的 剩余金额 但是在帐户表中的金额为操作后的金额,而资金变更表中的 剩余金额 确是帐户表中未操作前的金额(也就是二者不统一)我的代码为下:
if (rows["账号"].ToString() == textBoxInAccout.Text)
{
dr1["账号"] = textBoxInAccout.Text.ToString();
dr1["操作金额"] = textBoxRollManey.Text.ToString();
thisCommand.CommandText = "update 帐户表 set 金额 = 金额 +" + textBoxRollManey.Text+ "where 账号=" + textBoxInAccout.Text;
thisCommand.ExecuteNonQuery();
dr1["剩余金额"] = rows["金额"].ToString();
dr1["存取"] = "转入";
}
dr1["操作时间"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
thisDataSet1.Tables["资金变更表"].Rows.Add(dr);
thisAdapt1.Update(thisDataSet1, "资金变更表");

我想问一下那错了?

涉及到这种资金问题,为什么不用事务呢?

刚才给错东西了,重新发给你:
事务是一种机制,是一个操作序列,他包含了一组数据库操作命令,并且所有的命令作为一个整体一起向系统提交或者撤消操作请求,就是说这一组数据库命令要么都执行,要么都不执行!

下面一段银行的转帐的事务列子
事务的相关T-SQL:

use DataBase
..........
GO
/*------开始事务------*/
begin transcation
/*------定义变量------*/
DECLARE @errorSum int
set @errorSum = 0 --初始化为0元
/*-----转帐:张三的帐户上少1000元,李四帐户上多1000元---------*/
update bank set cMoney = cMoney -1000 where name = '张三'
set @errorSum = @errorSum - @@error ----累计是否有错误
update bank set cMoney = cMoney + 1000 where name = '李四'
set @errorSum = @errorSum - @@error ----累计是否有错误

print '查看事务后的余额'
select * from bank

/*----------根据有错还是没错,确定失误要提交还是回滚---------*/
if @errorSum <> 0 ---如果有错误
begin
print '交易失败,回滚事务'
rollback transcation
end
else
begin
print '交易成功,写入硬盘,保存'
rollbac