you can either do multiple conditions in the WHERE statement: WHERE id = x OR id = y etc
or you can use the in statement if you would rather pass in a group of values: WHERE id in (x,y,z, etc)
and, of course, you could combine them if that works better: WHERE id = x OR id in (a,b,c,d)
Obviously, the values from the datagrid would need to be passed in as parameters.
Not indeed. Construct the SQL Statement after completion of the loop.
The statement is as
Dim sqlstr As String = "Select * From YourTAbleNAme WHERE id = x OR id in " & x
You can also construct SQL Statement through loop like
Dim sqlStr As String = "Select * From TableName Where id = x OR id in ("
For Each r As DataGridViewRow In DataGridView1.Rows
sqlStr &= r.Cells(0).Value
If r.Index < DataGridView1.Rows.Count - 1 Then
sqlStr &= ", "
sqlStr &= ")"
Why don't you create a stored procedure to search for a record based on an ID passed in from the data grid view, instead of doing the query in your VB code?
It means all your stored procedures are in one place and have can be easily identified and modified.
It saves having to search through your VB code to find the string, can save you bulking your VB code out with query strings and is more managable in the long run. Especially when it comes to bigger application.