Home > database >  I need to create a query in windows forms c# having the where clause with Many id's stored in a
I need to create a query in windows forms c# having the where clause with Many id's stored in a

Time:12-17

An example of hard coded query that is working fine in sql: Select * From Products Where id_product in ( 1,3,5,6) Order by name;

What is in the parenthesis represents the elements of an int[] in C# / windows forms.

Please help me! I have no idea how to iterate throw the array after 'in'. :( It is the last last step in my project.

I am using data adapter, data source, sql commands.

Any brilliant idea to save my day? :))

Thank you in advance for your time! ;)

I tried a foreach loop but doesn't work. I am guessing that I should use parameter but still don't know how!?

CodePudding user response:

Man, this is the query that I want to use in c#:

Select all rows from the table that has the id in the ArrayList and sort it in alphabetical order. I am using Dapper. That's all

CodePudding user response:

You can try

var ids = new int[] {1, 2, 3, 4, 5};
var query = $"select * from products where product_id in ({string.Join(",", ids)}) order by name;";
  • Related