1

I am trying to delete data from a table using a SQLDataAdapter, and to do so I need to give it a DeleteCommand.

The SQL I would use to delete a row is:

DELETE FROM table WHERE ID = x

The problem is thus: How do I specify to the DataAdapter what to replace x with? The SQL to generate the DataAdapter is slightly different (no joins) than the data table it's being told to update (an outer join).

How can I do this?

2 Answers 2

4

Here you can pass parameter to delete command :

// Create the DeleteCommand.
command = new SqlCommand(
    "DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

// Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
      "@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;

adapter.DeleteCommand = command;

The code taken from MSDN

Sign up to request clarification or add additional context in comments.

2 Comments

Do I need to check for SQL Injection with this?
No you don't, you're passing inputs as parameters.
1

use SourceColumn and SourceVersion SqlParameter properties:

        var deleteCommand = connection.CreateCommand();
        deleteCommand = "DELETE FROM table WHERE ID = @ID";
        var param = new SqlParameter("ID");
        param.SourceColumn = "the Select Column";
        param.SourceVersion = DataRowVersion.Original;
        deleteCommand.Parameters.Add (param);

2 Comments

Can you explain what the SourceVersion is?
SourceVersion indicates which value would be used, Original is the original value that you get with the select query, Current is the current value of the field, like if you modified it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.