删除用户ID时删除其他表中的用户记录c# [英] Delete user records from others table when delete user ID c#

查看:100
本文介绍了删除用户ID时删除其他表中的用户记录c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




下面的代码工作正常,但我希望在我通过以下代码删除用户时删除所有记录,我需要为此代码添加更多功能数据库属于其他表中的相同UID:广告,简历,FavComp,FavJob,FavoCv,收藏夹,JobApplic,工作,订单,ShoppingCart。



Hi
The below code is working fine but i am looking to add more feature to this code as i need when i delete the user via below code as well delete all records into database belong to the same UID in others tables: ads, CVs, FavComp, FavJob, FavoCv, favourite, JobApplic, jobs, Orders, ShoppingCart.

protected void DeltUsrbtn_Command(object sender, CommandEventArgs e)
        {
            using (SqlConnection DeltUsrSQLCon = new SqlConnection(sc))
            {
                System.Data.SqlClient.SqlCommand DeltUsrcmd = new System.Data.SqlClient.SqlCommand();
                DeltUsrcmd.CommandType = System.Data.CommandType.Text;

                DeltUsrcmd.CommandText = "DELETE FROM UserInfo WHERE UID = @UID";

                DeltUsrcmd.Parameters.AddWithValue("@UID", e.CommandArgument.ToString());
                DeltUsrcmd.Connection = DeltUsrSQLCon;
                DeltUsrSQLCon.Open();

                DeltUsrcmd.ExecuteNonQuery();
                DeltUsrSQLCon.Close();
                ReBindDeltUsrsGV();
                admusrdeletedlbl.Text = "User has been deleted successfully";
            }
        }

推荐答案

有很多方法可以做到这一点,但因为我们正在谈论这个功能然后解决方案是,(正如Arkadeep在他对你的问题的评论中所说),也为其他表添加删除命令。一旦从一个表中删除了数据,它也将从其他表中删除数据。



There are many ways to do this, but since we are talking about this function then the solution would be, (as Arkadeep said in his comment to your question), to add the delete commands for other tables too. Which would delete the data from other tables also, once it has finished deleting from one table.

SqlCommand command = new SqlCommand("DELETE FROM ads WHERE UID = @UID", connection);
SqlCommand command = new SqlCommand("DELETE FROM CVs WHERE UID = @UID", connection);
SqlCommand command = new SqlCommand("DELETE FROM FavComp WHERE UID = @UID", connection);
SqlCommand command = new SqlCommand("DELETE FROM FavJob WHERE UID = @UID", connection);
...





这样,这些命令也会删除其他表中具有该ID的用户的记录。



另一种方法是实施参照完整性 [< a href =https://en.wikipedia.org/wiki/Referential_target =_ blanktitle =New Window> ^ ]并将UserInfo存储为主表。这样孩子就可以保存主表中存在的那些记录的数据(仅!)。如果master在记录中删除或更新,则其他表将根据新数据自行更新。 这种方法需要花费一些时间,因为您可能需要首先了解这个概念



This way, these commands would delete the record for the user with that ID in other tables also.

Another way would be to implement referential integrity[^] and store the UserInfo as Master table. So that the child would hold the data for those records (only!) which exist in the master table. If master deletes or updates on record, other tables update themselves based on the new data. This method would take a bit of time as you may need to learn about the concept first.


这篇关于删除用户ID时删除其他表中的用户记录c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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