Click here to Skip to main content
15,881,819 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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.
 
Share this answer
 
v2
Comments
Espen Harlinn 15-Jul-11 6:34am    
Good point, my 5
Reiss 15-Jul-11 9:07am    
SQL injection will be a threat if you allow direct text sql execution against your database
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");

This code may help you.
 
Share this answer
 
v4
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.
 
Share this answer
 
Comments
Reiss 15-Jul-11 9:03am    
On top of this I would suggest that you read this article http://www.akadia.com/services/sqlsrv_table_auditing.html on creating triggers to audit changes made to the data.
i have done it :

public int deleteRecord(string tablename,param string []clause)
{

string str="delete from " + tablename + " where clause[i];

}

implement your logic
 
Share this answer
 
v2
Comments
Manas Bhardwaj 15-Jul-11 8:04am    
possible sql injection:

string tablename = "sometable";
string[] clause = new string[] () {"1 = 1"};

deleteRecord(tablename, clause);
Sudhir Kumar Srivastava lko 15-Jul-11 8:40am    
i know.
Uday P.Singh 15-Jul-11 15:38pm    
good point by Manas, but nevertheless worth 5!
//create a class in the name of DButility file with some methods which are u want like bellow



MIDL
public class DbUtility
{
    public DbUtility()
    {
        //
        // TODO: Add constructor logic here
        //
    }


C#
public static SqlConnection OpenConnection()
    {
        SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["YouConnectionString"].ConnectionString);
        if (cnn.State == ConnectionState.Open)
        {
            cnn.Close();
            cnn.Open();
        }
        else
        {
            cnn.Open();
        }
        return cnn;
    }



// then create a method for delete your record like follow

C#
public static int DeleteRecord(string query)
    {
        int i = 0;
        SqlCommand com = new SqlCommand();
        com = new SqlCommand(query, DbUtility.OpenConnection());
        i = com.ExecuteNonQuery();
        DbUtility.CloseConnection();
        return i;
    }




// now if u want to delete your re the just write the bellow code for delete button click


C#
protected void button_Delete_Click(object sender, EventArgs e)
    {
        string qry = "delete * from table_xyz";
        DbUtility.DeleteRecord(qry);
    }

}


// just u need to write only query in cs page...
 
Share this answer
 
v2
In pratice, it is often better to have specific code for each tables as it help ensure that you pass the appropriate parameter for the table.

In fact, often code generated by Visual Studio designer already provide a way to delete a row from a table. It might not be as optimal as hand generated code but generally, it will be somewhat safer to use.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900