Hello everyone, I need to display in the DataGridView from the MS SQL database table a specific set of rows that are written in a file. With the output from the file, I myself, tell me how to display a specific line?


Example: There is a dbo.test table from it that you need to load into the DataGridView, do not touch the rest of the lines 1 and 5.

  • Maybe by “line” you mean lines with id = 1 and id = 5 (where id is the primary key of the table)? And just as the file contains numbers on each line? - AK
  • Yes, I mean it. - Joker
  • then you need to use the WHERE id IN (1,5,17) construct and understand that the IN construct is very inefficient. - AK

1 answer 1

In the database there is no such "1 and 5 line". Rows are stored sorted by cluster index (if there is one), which normally corresponds to the primary key. If during the selection you want to get the rows in a certain order, then specify ORDER BY , otherwise the order of the received records is not defined and can be any.

A key is used to select specific rows. To select only two records, use the IN (1, 5) construct.

In general, the answer to your question may be close to something like this - here, lines 1 and 5 are selected, in the order defined by the field Key1:

 SELECT *, ROW_NUMBER() OVER (ORDER BY Key1) AS [RowNumber] FROM dbo.test WHERE [RowNumber] IN (1, 5) ORDER BY Key1 
  • This is if there is a clustered index, and not everything in the heap (brrr!) Rolls around. Then the lines are generally stored as. - AK
  • The author of the question accepted your answer, and then nevertheless clarified the formulation of the problem. He does not need such difficulties with line numbering, if not difficult: add a couple of lines in response. - AK