-1

I am trying to create a C# delete function. This is what I have so far, although I want to adapt it to work like my pseudo code below.

Pseudo code for new function

//public bool delete(Dictionary <string, string> id, string tableName)
//{

//reader or another object

//open conenction 

//statement takes input of the primary key of the row to be edited
//as an array of one, the value plus the name of the column representing the primary key
//for the row to be deleted, as well as the table name 


//connection

//excute 

//return bool? 

//}
//}

What I've tried:

public bool Delete(Dictionary <string, string> id, string tableName)
{            
    //reader or another object            

    //open connection 
    using (SqlConnection conn = new SqlConnection(ConnectionString))
    {    
        conn.Open();
        using (SqlCommand cmd = new SqlCommand("DELETE FROM tableName WHERE id=id", conn))
        {
            cmd.ExecuteNonQuery();
        }              
    }
}

But when doing this I am struggling with adapting the code and would appreciate some help please. I can provide more info if needed. I want the new function to take the input of the unique identifier of the row to be edited as an array of one – the value plus the name of the column representing the primary key - for the row to be deleted, as well as the table name.

Michael McGriff
  • 793
  • 10
  • 20
blueboy123
  • 47
  • 1
  • 1
  • 8
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – Albireo Dec 17 '15 at 14:49
  • Okay so the problem i am encountering is how to adjust my current code to fit the new purpose, the part i am struggling with is the statement. – blueboy123 Dec 17 '15 at 14:54
  • Possible duplicate of [sqlcommand execute query not updating deleting and inserting](http://stackoverflow.com/questions/11344528/sqlcommand-execute-query-not-updating-deleting-and-inserting) – blas3nik Dec 17 '15 at 14:56
  • 1
    All of your questions that I've seen so far are basic syntax questions. Have you read the supporting documentation? [MSDN](https://msdn.microsoft.com/en-us/library/ms233823.aspx) is pretty useful once you get used to reading it. Questions about syntax which can easily be solved via a simple Google search are generally poorly received here, especially as there are many duplicates even here on this site. – Michael McGriff Dec 17 '15 at 15:23
  • Hi Michael thanks for your help, okay I will look at that now, although I think that these functions are slightly harder then the basic. – blueboy123 Dec 17 '15 at 15:57

1 Answers1

2

You need to take the values from your arguments and put them into a correctly formatted sql string, like so:

public bool Delete(string id, string column, string table)
{
  using (SqlConnection conn = new SqlConnection(ConnectionString))
  {
     conn.Open();
     var query = String.Format("DELETE FROM {0} WHERE {1}='{2}'", table,column, id)
     using (SqlCommand cmd = new SqlCommand(query))
     {
          cmd.ExecuteNonQuery();
     }
  }
}

This approach has many performance and security issues, but I take it you are doing this for educational purposes?

Mikael Nitell
  • 1,069
  • 6
  • 16
  • Okay i will give that a go, yep i don't mind about that at this stage. It says not all code paths are returning a value so how would i return a boll en at the end to say that delete has worked? – blueboy123 Dec 17 '15 at 15:13
  • Yeah, that makes sense, or make the method void for now. – Mikael Nitell Dec 17 '15 at 15:33
  • The thing is i have to do it with public bool Delete(Dictionary id, string tableName) so I can't use that method but thanks for your help – blueboy123 Dec 17 '15 at 16:38