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

4

1 に答える 1

0

配列に格納されている値に従ってデータベースにクエリを実行するだけです。

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

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

クエリは次のようになります。

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

PS: SQL インジェクションの可能性があるため、このクエリのユーザー入力を受け入れないでください :-)

于 2013-01-18T18:24:24.373 に答える