.net的数据同步更新问题,期望大家给以解释。

gbliang 2005-08-13 03:49:43
ado.net的adpter实行的是脱机管理机制,那么,如果A和B两个用户都在使用同一条记录,A用户根据这条记录的各个字段计算一个总和。而B用户在此时却更改了这条记录。那么,A用户得到的总和就是个错误的值,请问有什么好的方法解决吗?各位专家,摆脱了。
...全文
226 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
cityhunter172 2005-08-16
  • 打赏
  • 举报
回复
连线更新时
在 Table 中加一个 timestamp 类型的列(即为“时间戳”, SQL 2000 的用法;Oracle 中没试过),修改资料时把“时间戳”作为 where 的条件。如果不相同,则说明此行被其他用户修改过。

若已删除 ExecuteNonQuery() 肯定返回 0 啦



离线更新的话,就把原来的值保存,更新时再把原值写进 where 条件中,如

update Table2
set num = 199
where id = 102 and num = 32;
gbliang 2005-08-16
  • 打赏
  • 举报
回复
大家踊跃发言阿
wingnal 2005-08-14
  • 打赏
  • 举报
回复
乐观锁和悲观锁
如果你想避免出现这种情况  则必须采用悲观锁
即a用户在访问时应该将数据锁定 禁止其他用户修改
gbliang 2005-08-14
  • 打赏
  • 举报
回复
谢谢大家的介绍,欢迎大家继续讨论
zhilunchen 2005-08-13
  • 打赏
  • 举报
回复
使用时间戳可以解决这个问题,即在表中加一个时间戳字段,具体控制参见:
<<.net企业应用高级编程--C#编程篇>>一书
kensou007 2005-08-13
  • 打赏
  • 举报
回复
我觉得这个担心没必要啊,假设甲乙两个人操作数据库中的Table A。首先他们各自把TableA读入dataset,这时就是脱机操作了,(For example:甲删除了Table A里的记录B,乙更改了记录B)完全在内存中进行数据变化。甲先执行DataAdapter.update(),数据库就把记录B删除了。此时乙再执行DataAdapter.update(),数据库中就找不到该条记录了,此时会出异常,当然你在程序中要catch(所有与数据库打交道的代码都要try&catch,包括连接,更新。。。操作)这时候就会捕捉到异常,这个完全可以由后台数据库自动事务控制,一点问题也没有。
极客行天下 2005-08-13
  • 打赏
  • 举报
回复
这种问题的确存在,不过在实际应用中好象并不感觉很严重.
navy_koo 2005-08-13
  • 打赏
  • 举报
回复
如果对业务会造成影响,当然要进行锁表、事务了!
ythomas 2005-08-13
  • 打赏
  • 举报
回复
generally speaking,we can deal with this kind of situation in two ways:

(1)Optimistic concurrency control—With this strategy, an update to data succeeds only if no one else has changed that row after it is loaded into the DataSet object.

(2)“Last one wins” concurrency control—With this strategy, an update to data always succeeds, whether another user has edited the data or not (as long as the row still exists).

The following is one "last one wins" example:

UPDATE Customers
SET ContactName = @ContactName
WHERE CustomerID = @CustomerID

The key thing to look at here is the WHERE clause. The only column that it looks at is the CustomerID column. CustomerID is the primary key of this table, a value that should never change. As long as that one column has not been changed, the UPDATE statement succeeds,no matter what may have changed about other columns in the same table

The following is an example for the optimistic concurrency control

try
{
da.Update(ds, “Customers”);
MessageBox.Show(“Contact Name Updated!”);
}
catch(SqlException sqlEx)
{
foreach (SqlError err in sqlEx.Errors){MessageBox.Show(“SQL Error “ + err.Number + “: “ + err.Message);}
}
catch(Exception ex)
{
// Handle general errors
MessageBox.Show(“Non-SQL Exception “ + ex.Message);
}

The difference between the two versions of the update code is the inclusion of a try-catch block to handle exceptions and the UPDATE SQL statement, which now has a different WHERE clause:
UPDATE Customers
SET ContactName = @ContactName
WHERE CustomerID = @CustomerID AND ContactName = @ContactNameOrig

The new WHERE clause finds a row to update only if both the CustomerID and ContactName fields are unchanged from what they were when the row was originally loaded.

for more information,you guys could search "Optimistic concurrency control" at google or codeproject.


山中老狼 2005-08-13
  • 打赏
  • 举报
回复
调用事务锁

也可以自己控制......
ioul 2005-08-13
  • 打赏
  • 举报
回复
如果用锁定的话,当很多人要访问你的数据时就会产生延时,这样访问的速度就变慢了,不如用个隐藏的祯动态的刷新数据,每秒传递最新的数据。
xboard 2005-08-13
  • 打赏
  • 举报
回复
用锁定(lock)来进行。

当A用户对记录操作的时候。锁定这条记录。不允许其它用户操作。操作完成在释放锁定。B用户就可以对这条记录进行操作了
hsg11804 2005-08-13
  • 打赏
  • 举报
回复
当A在操作时,锁定数据库中的该条记录.这样B就不能改了.
asp里面有,但在.NET没有研究过.
fphuang 2005-08-13
  • 打赏
  • 举报
回复
这个在C/S下面是经常需要考虑的一个问题,不过B/S如果要考虑这个问题,我想除非你的系统是在一个并不太大的范围内使用,否则很难,比如你可以专门用一个表来存放状态,即如果有人在改动某个标的时候,就将这个标的表是写在这个专门的表李,而每个人在有改动之前都需要先读一下这个表,看看是不是可以改动,大概是这个意思。
hchxxzx 2005-08-13
  • 打赏
  • 举报
回复
这个恐怕没有办法啊.
在用户查看的时候,他查看的是当时的数据,而这个数据离实时动态的数据,已经非常接近了
不过,虽然是脱机,但只要用户有提交页面的动作,那么你仍然可以再次执行取数据的动作,如此,情况会好很多.这个脱机只是指数据取到之后就断开连接,并不是说它一直存在(当然要一直存在也是可以通过其他方法实现)
俊哥V 2005-08-13
  • 打赏
  • 举报
回复
用锁
改用 DataReader


来学习好的解决方法
ark_matrix 2005-08-13
  • 打赏
  • 举报
回复
忘了在那里看过的,好像用时间戳可以解决这个问题
Visual_Studio_Net 2005-08-13
  • 打赏
  • 举报
回复
帮你UP,偶业想知道类似的问题

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧