Run a SQL Select on a DataTable?

  • Thread starter Thread starter William Ryan
  • Start date Start date
I would like to run the equivalent of a SQL Select (like "SELECT Employees,
EmpNum WHERE...") on a DataTable and place the results into a new DataTable.
I originally guessed that it would be natural for ADO.NET to provide a
member of the DataTable class to do this, but I'm finding no such thing in
the documentation. I see that the DataTable.Select() method can do a
similar but very limited funcion, but does not give the flexibility of a SQL
SELECT statement. Also, it stuck me as a bit odd that DataTable.Select()
returns an array of DataRows rather than a DataTable, which is what I would
have expected... hmmm. (I still have alot to learn about the zen of
ADO.NET, but I'm getting there.)

Can anyone recommend a way to run a SQL SELECT on a DataTable? Or the next
best way(s) to achieve the equivalent results?

Cheers, Bruce
 
Bruce,

Yes, I agree with you - it is very limited. Here is how I do it:

Dim dr() As DataRow = dt.Select("CompanyName = ....")

Dim i As Integer
For i = 0 To dr.Length - 1
dtCopy.BeginLoadData()
dtCopy.LoadDataRow(dr(i).ItemArray, False)
dtCopy.EndLoadData()
Next

I hope this helps!


Thanks,
Hussein Abuthuraya
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.
 
Look at the Select method on DataTable. It returns an array of DataRows.
Then, look at the ImportRow method on DataTable to get these rows into
another DataTable.
 
Back
Top