更新数据时出现问题 [英] Problem in updating data

查看:112
本文介绍了更新数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已经制作了一个使用sql server保留数据并在需要时检索它的应用程序.今天我和我的朋友尝试了
从不同的计算机更新同一记录的数据,则抛出异常.我已经阅读了很多有关乐观并发"的信息,但是老实说,我不知道如何实现它.谁能帮我或至少指导我
关于如何实施它.

谢谢和问候
ajinkya

Hey guys,

I have made an application that uses sql server to keep data and retrieve it when needed. Today me and my friend tried
to update data of same record from different computers, it threw exception. I have read a lot about ''Optimistic concurrency'' but to be honest, I don''t know how to implement it. Can anyone please help me or atleast guide me
on how should i implement it.

Thanks and regards
ajinkya

推荐答案

大多数数据层"都有某种检查,以确保在您提交对数据库的更改时记录是否仍然相同. br/> 例如,您从数据库中检索包含"a"的记录,然后将其更改为"b".当您将其提交到数据库时,您执行的命令将检查"a"并将其更新为"b".现在,如果同时有人将数据库中的"a"更改为其他值(例如"c"),则命令检查"a"并将其更改为"b"将不会有任何结果,因为"a"已经不存在(因为它已经更改为"c").

您的代码知道"有更改,期望您的数据库更新1条记录,但是数据库返回0条更改记录的结果.这正是您的错误的意思.

您需要根据某些选择找到一种处理此类更改的方法.例如,您可以选择从数据库中检索记录,更新用户的更改并将其立即写回到数据库中,但是有可能您将撤消在第一笔交易中所做的更改.

您可能想看看 [
Most data ''layers'' have some kind of check to see if the record is still the same by the time you commit changes to the database.
For example, you retrieve a record from the database containing ''a'', and you change it to ''b''. When you commit that to the database, the command you execute will check for ''a'' and update that to ''b''. Now if somebody in the meantime changes the ''a'' in the database to a different value (say ''c'', the command check for ''a'' and change it to ''b'' will result in nothing, because ''a'' doesn''t exist anymore (because it''s already changed to ''c'').

Your code ''knows'' there''s a change an expects your database to update 1 record, but the database returns a result of 0 changed records. That is exactly what your error means.

You need to find a way to handle that kind of changes depending on some choises. You can choose for example to retrieve the record from the database, update the user''s changes and immidiately write it back to the database, but there''s a chance you''ll undo the changes made in the first transaction.

You may want to take a peek at this[^] page. It explains way better what''s going on than I can.

Good luck!

Eduard


您需要阅读SQL Locking.

请仔细阅读以下内容:
如何解决由SQL Server中的锁升级引起的阻塞问题 [高级SQL Server锁定 [减少SQL Server锁定 [了解SQL Server中的锁定 [在Microsoft SQL Server中锁定 [ SQL服务器锁定 [ ^ ]
You need to read on SQL Locking.

Please go through them:
How to resolve blocking problems that are caused by lock escalation in SQL Server[^]
Advanced SQL Server Locking[^]
Reducing SQL Server Locks[^]
Understanding Locking in SQL Server[^]
Locking in Microsoft SQL Server[^]
SQL server locking[^]


字符串ConStr = ConfigurationSettings.AppSettings ["DATA_BASE"];
私人void control_click(对象发送者...)
{
SqlConnection con =新的SqlConnection(ConStr);
stirng Sql ="UPDATE TBL_NAME设置(列)值(@PARAMETER)<如果需要条件在何处>";
SqlCommand cmd =新的SqlCommand(Sql,con);

cmd.Paramenter.AddWithValue("@ PARAMENTER",TXT.text);

试试
{
cmd.ExecuteNonQuery();
MessageBox(成功更新");
}
赶上
{
MessageBox(更新时发生错误");
}
}
string ConStr = ConfigurationSettings.AppSettings["DATA_BASE"];
private void control_click(object sender...)
{
SqlConnection con = new SqlConnection(ConStr);
stirng Sql="UPDATE TBL_NAME SET (COLUMN) VALUES (@PARAMETER) <WHERE CONDITION IF REQUIRED>";
SqlCommand cmd = new SqlCommand(Sql, con);

cmd.Paramenter.AddWithValue("@PARAMENTER", TXT.text);

try
{
cmd.ExecuteNonQuery();
MessageBox("Updated Successfully");
}
catch
{
MessageBox("Error occured while updating");
}
}


这篇关于更新数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆