在C#中由其他用户刷新数据库中数据更改的前端 [英] Refresh Front-End on Data Change in Database by Other User in C#

查看:83
本文介绍了在C#中由其他用户刷新数据库中数据更改的前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#作为前端,有多个用户,它与Ms Sql Server 2008连接。我的问题是,如果一个用户使用更新选项更改数据。如何在不使用Select *命令或sda.fill的情况下,其他用户如何看到该更新语句的影响?我的表中有成千上万的记录,这需要花费太多时间来重新填充。

这是我用来更新数据的代码。

I am using C# as Front End with multiple users, which is connected with Ms Sql Server 2008. My Question is, If one user changes the data using an update option. how can other user see the impact of that update statement without using Select * command or sda.fill? I have thousands of records in my table which takes too much time on refill.
Here is code i am using to update data.

MyGlobalVariables.bsInc.EndEdit();

MyGlobalVariables.con.Open();

MyGlobalVariables.CmdString = "Update Increment set IncAmount=" + Convert.ToInt32(txtInc.Text) + ", EditedBy='"+MyGlobalVariables.MyUname+"', editedOn=sysdatetime() where IncYear=" + Convert.ToInt16(cmbIncYear.Text) + " and EmpCode='" + MyGlobalVariables.MyEmpCode + "'";

MyGlobalVariables.cmd = new SqlCommand(MyGlobalVariables.CmdString, MyGlobalVariables.con);

MyGlobalVariables.cmd.ExecuteNonQuery();

MyGlobalVariables.con.Close();

MyGlobalVariables.ds.Tables["Increment"].Rows[MyGlobalVariables.bsInc.Position].AcceptChanges();

MyGlobalVariables.MyParentForm.lblProg.Text = "Record Updated Successfully.";

推荐答案

使用 SqlDependency类 [ ^ ]。



请参阅:

使用SqlDependency进行数据更改事件 [ ^ ]

使用SqlDependency和SqlCacheDependency查询通知 [ ^ ]



BTW:您的代码是 Sql Injection [ ^ ]易受攻击。请参考:如何:防止ASP.NET中的SQL注入 [ ^ ]
Use SqlDependency class[^].

See:
Using SqlDependency for data change events[^]
Query Notification using SqlDependency and SqlCacheDependency[^]

BTW: your code is Sql Injection[^] vulnerable. Please refer this: How To: Protect From SQL Injection in ASP.NET[^]


通过Allah Almighty的Grace,我找到了解决问题的方法。

这是插入过程后的我的代码。

By the Grace of Allah Almighty I have found the solution of my problem.
Here is My Code after Insert Process.
SqlDataAdapter sdaTemp = new SqlDataAdapter();
DataTable dtTemp = new DataTable();
MyGlobalVariables.con.Open();
sdaTemp = new SqlDataAdapter("Select * from Increment where ecode=(select MAX(ecode) from Increment) and UserID='"+MyGlobalVariables.MyUid+"'", MyGlobalVariables.con);
MyGlobalVariables.con.Close();
sdaTemp.Fill(dtTemp);
MyGlobalVariables.dtInc.Merge(dtTemp);
MyGlobalVariables.dtInc.AcceptChanges();
MyGlobalVariables.bsInc.MoveLast();





感谢每个人的建议。



Thanks every body for your suggestions.


这篇关于在C#中由其他用户刷新数据库中数据更改的前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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