开发者

.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 si开发者_如何学运维milar 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#

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜