简体   繁体   中英

.NET - Can I use SQL code on an existing Datatable?

I'd like to utilize Select & Insert statements on a DataTable in VB.NET or C#.

Example:

Dim Results as DataTable 
Results = Select * from SourceDataTable where PlayerID < 10

Is anything similar to this possible?

Thanks

If you're using .NET 3.5 then you can use LINQ (I don't know the VB.NET syntax, so here's C#):

var results = from row in SourceDataTable.AsEnumerable()
              where row.Field<int>("PlayerID") < 10
              select row;

It's not exactly the same, but certainly pretty close to "natural" SQL syntax. LINQ also has the advantage that it works on any collection type (and even directly against the database, using LINQ-to-SQL), not just DataTables.

DataTables have a select method which return an array of DataRow

the filterExpression and sort paramaters take sql;

result = SourceDataTable.Select(" PlayerID < 10 ")

For select use

DataTable dt ;
dt = dt.Select("PlayerID < 10");

in C#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM