---
title: "Loop through ado.net sql select to add more rows to datatable"  
description: "Loop through ado.net sql select to add more rows to datatable"  
author: "Anonymous User"  
published: 2013-06-19  
updated: 2013-06-19  
canonical: https://www.mindstick.com/forum/1220/loop-through-ado-dot-net-sql-select-to-add-more-rows-to-datatable  
category: "ado.net"  
tags: ["ado.net"]  
reading_time: 2 minutes  

---

# Loop through ado.net sql select to add more rows to datatable

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances), \
\
I currently have a working query for the first element in the array of ID's as seen below. What I need to do is add a [for loop](https://www.mindstick.com/forum/12568/android-for-loop-not-working-in-main-thread) so I rerun the query for every element in the array and add each new row to the [datatable](https://www.mindstick.com/blog/195/datatable-in-ado-dot-net) but I am not sure how I do this? Unless there is a way I can include all ID's of my array in the [where clause](https://www.mindstick.com/interview/1909/when-do-you-use-where-clause-and-when-do-you-use-having-clause) so I [retrieve](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) all rows through first run.\
\
PID[] is a [string array](https://www.mindstick.com/forum/12803/check-for-any-item-in-string-array) and could have anywhere from 1 to 5 [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements) that are [random](https://www.mindstick.com/forum/33419/generating-random-numbers-in-objective-c) ID's.\
\
Any help would be appreciated!\
\
for loop here?\
\

```
        string firstQuery = "select * from Property p " + "where p.id in (@pID)";                              connString.Open();        SqlCommand selectAll = new SqlCommand(firstQuery, connString);        selectAll.Parameters.AddWithValue("@pID", PID[0]);        SqlDataAdapter adapter = new SqlDataAdapter();        adapter.SelectCommand = selectAll;        DataSet ds = new DataSet();        adapter.Fill(ds);        connString.Close();        DataTable table = ds.Tables[0];
```

\
Any help would be amazing thank you!

## Replies

### Reply by Sumit Kesarwani

Hi Jayprakash,\
Yes you can include all ids in one parameter and get results that match them:\

```
var parameters = new string[PID.Length];var selectAll = new SqlCommand();for (int i = 0; i < PID.Length; i++){    parameters[i] = string.Format("@Age{0}", i);    selectAll .Parameters.AddWithValue(parameters[i], PID[i]);}selectAll.CommandText = string.Format("SELECT * from Property p WHERE p.id IN ({0})", string.Join(", ", parameters));selectAll.Connection = connString;connString.Open();SqlDataAdapter adapter = new SqlDataAdapter();adapter.SelectCommand = selectAll;DataSet ds = new DataSet();adapter.Fill(ds);connString.Close();DataTable table = ds.Tables[0];
```


---

Original Source: https://www.mindstick.com/forum/1220/loop-through-ado-dot-net-sql-select-to-add-more-rows-to-datatable

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
