1

I have a list List<Int32> containing ids and want to select some values from another table where id=ids[0],ids[1],...

it look like this:

 string query=String.Format("@ SELECT values from Table WHERE id=???");

How to get result?

P.S. as i listen- that this way is not right.

So, another way to do that- use Join:

   string queryString = String.Format(@" SELECT * FROM Table1 [t1]
                                           join [Table2] [t2]  
                                           on [t1].idTable1=[t2].id where [idParamValue]={0}",  idParamValue);

So, then i should use :

using (var sqlCmd = new SqlCommand(queryString, _connection))
        {
            using (var sqlReader = sqlCmd.ExecuteReader())
            {
                while (sqlReader.Read())
                {
                    var param1=(String)sqlReader["param_name"];                 
                }
            }
        }

Thank you!

user2545071
  • 1,250
  • 2
  • 19
  • 43
  • What version of sql are you running, the ways to solve this change if you are using Sql2008 or newer. – Scott Chamberlain Aug 11 '14 at 05:34
  • 1
    What you need is to "join" the other table. http://www.w3schools.com/sql/sql_join.asp – nixn Aug 11 '14 at 05:35
  • Are you using Entity framework in your apploication? – Rahul Aug 11 '14 at 05:46
  • question is not clear enough.... according to your question the answer can be found in http://stackoverflow.com/questions/5803472/sql-where-id-in-id1-id2-idn – Suji Aug 11 '14 at 06:11

2 Answers2

0
SELECT * FROM Table WHERE id IN (79,86,42)
Vignesh Kumar A
  • 26,578
  • 11
  • 57
  • 101
Ty H.
  • 875
  • 6
  • 8
-3

You can use below query :-

string query=String.Format("@ SELECT values from Table WHERE id in(value1,value2,value3,value4,...)");
Vignesh Kumar A
  • 26,578
  • 11
  • 57
  • 101
Amul Harad
  • 128
  • 4