0

I'm new in C# and I have an array with different values as shown below

int[] array = { 1, 2, 3, 4, 5, 6 };

I need to do is to relate the value of the array with the id of a data in my database in Sqlserver, Example, the first value for my array is 1, and the value from my data Id is also 1, I need show in a DataGrid the the name or information that this id holds.

DataBase Example:

  • id 1 , name Francisco, serial_number 1234

  • id 2 , name Claudio, serial_number 4321

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1974277
  • 77
  • 1
  • 3
  • 11

1 Answers1

0

Just Query the database according to the values stored in your array.

int[] array = { 1, 2, 3, 4, 5, 6 };
string sql = "";

foreach (int id in array)
{
   // append your ids to variable sql seperated by commas
}

now your query should be for example :

"Select * from Table1 WHERE ID in (" + sql + ")"

PS: Don't accept user input for this query as it would be a potential SQL injection :-)

Abdusalam Ben Haj
  • 5,343
  • 5
  • 31
  • 45