C# : Tutorial of Asp.net with c# and example

Example and code about windows application, web application, SQL, C#, WPF etc which is helpful to developer and programer.

Search This Blog

SQL: 'IN' Query example in SQL with c#

♠ Posted by Unknown in at Thursday, October 09, 2014

Example of 'in Query' in SQL Server Management studio 2008 with C#

Whenever you fire where condition in SQL you need give some value in where condition some time you need to only assign only single value or some times you need to assign number of value in where condition that time IN Query is most useful. 

there may be in database comma sepreted  value in single field and you need to right in Where condition that time IN Query is most useful to you.

For Example In below code First make one string using foreach loop from datatable which is comma sepreted and ten last statement contain SQL Query which contain Where condition. 

here Where condition on empid field which compare number of empid at a time which is comma sepreted.

string var = "";
foreach (DataRow row in table.Rows)
var += row["emp_no"].ToString() + ",";
if (var.EndsWith(","))
var = var.Trim().Substring(0, var.Trim().Length - 1);
//do sql connection
string value1 = sql.GetSingleValue("select empuniqid from Empmaster where empid in ( " + var + " ) ");

Improve Your Answer as comment below.
If our post is useful please share and comment on our post for more post and detail Visit :



Post a Comment