创建删除记录的通用功能. [英] create a common function for deleting records .

查看:70
本文介绍了创建删除记录的通用功能.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想创建一个C#.net方法来删除表中的记录,我会将表名作为参数传递,但是对于每个表,where子句都会有所不同,您能否向我建议代码.

该怎么做?


在此先感谢


Sudhir Srivastava

Hi All,


I want to create a C#.net method for deleting the records from table i would pass table name as parameter but the where clause would be different for every table, can u plz suggest me the code.

How to do this ?


Thanks in advance


Sudhir Srivastava

推荐答案

我建​​议您宁愿创建专用的存储过程来从不同表中删除数据.通过创建一个函数(虽然可以实现),您就可以避免进行SQL注入的风险.
I would suggest you to rather create dedicated Stored Procedures for deleting data from different tables. By creating one function (which is possible though), you leave the risk of SQL injection.


public void deleterecords(string tablename,string id,string fieldname)
{
string m_str;
 m_Str = "delete from "+tablename +"where "+fieldname+"="+id;
            int intResult = new Class1().SqlConExecuteNonQuery(m_Str);
}





deleterecords("PersonalDetails","1","Id");

这段代码可能会对您有所帮助.





deleterecords("PersonalDetails","1","Id");

This code may help you.


我是否可以建议您实际上并不删除记录,而是通过在记录上使用标记将它们标记为已删除?如果这样做,则可以很轻松地在应用程序中保留引用完整性,并且可以以更直接的方式提供审核功能.

考虑以下情况:您有一个正在通过应用程序管理的查找表.如果用户决定删除一条记录,则不必担心删除与该记录关联的所有其他记录-并可能导致需要执行一系列删除操作(这可能会带来额外的副作用)删除仍应处于活动状态的数据).

显然,这意味着读入它们时必须过滤掉已删除"的记录,但这并不是一项艰巨的任务.
May I suggest that you don''t actually delete the records, but rather mark them as being deleted through the use of a flag on a record? If you do this, you make it a lot easier for yourself to preserve referential integrity in your application, and that you can provide auditing functionality in a much more straight forward way.

Consider the case where you have a lookup table that you are managing through your application. If the user decides that a record should be deleted, you don''t have to worry about deleting all the other records associated with that record - and potentially causing a cascade of deletes that need to be performed (which could have the added side effect of removing data that should still be active).

Obviously this means that you have to filter out the "deleted" records when you read them in, but that''s not an overly difficult task.


这篇关于创建删除记录的通用功能.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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