0

Challenging issue you have encountered already ?

This is my challenge.

I have the query as follows - MSSQL Server 2017 calling from my C# application.

SQL

update slides set isBorrowed=0, updated_by='Daniel'
where id in (1,2,3);

C# code to achieve above

'Update Slides SET isBorrowed=1,updated_by=@update_by WHERE id IN (@ids)'

   string updateDonorQuery = "UPDATE slides " +
                            "SET isBorrowed=1,updated_by=@updated_by WHERE id IN (@ids)";
                        command.CommandText = updateDonorQuery;
                        command.CommandType = CommandType.Text;
                   command.Parameters.AddWithValue("@ids",list_ids.Split(',').Select(int.Parse).toList());
                   command.Parameters.AddWithValue("@updated_by", "Full name

Now The error is saying : "Exception on my command not expecting to receive list"

Then , i have changed to a string like this:

string list_ids = "1,2,3";

command.Parameters.AddWithValue("@ids",list_ids);
command.Parameters.AddWithValue("@updated_by", "Full name")

Agin code throw exception that complainant i have passed in nvarchar values instead of int. I guess interested the code as '1,2,3' when getting ids.

Guys i need a breakthrough , i hate loops.

6
  • Pass another parameter '@updated_by'. Commented Feb 24, 2020 at 12:28
  • it is passed let me edit the code Commented Feb 24, 2020 at 12:29
  • You could create a table-type variable and pass the values as a table parameter. Commented Feb 24, 2020 at 12:31
  • Does this answer your question? Pass Array Parameter in SqlCommand Commented Feb 24, 2020 at 12:32
  • 6
    Your question conflicts with the sql-server-2008 tag. If you are using SQL 2017, simply change the IN clause in the query to IN(SELECT value FROM STRING_SPLIT(@ids,',')). Also, avoid AddWithValue. Commented Feb 24, 2020 at 12:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.