开发者

How to Run queries on datasets

I wish to run que开发者_Python百科ry on datasets.

Is it possible.

If yes please give me an example and if not please suggest alternative solution.


You can use LINQ to DataSet. There are plenty of general examples here.


You can't run full SQL queries on DataSets but there are a couple of options:

(1) You can filter them using the DataSet.Select() method:

http://msdn.microsoft.com/en-us/library/det4aw50.aspx

DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression;
expression = "Date > #1/1/00#";
DataRow[] foundRows;

// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);

(2) You can use LINQ to DataSet.

DataTable orders = ds.Tables["SalesOrderHeader"];

var query =
    from order in orders.AsEnumerable()
    where order.Field<bool>("OnlineOrderFlag") == true
    select new
    {
        SalesOrderID = order.Field<int>("SalesOrderID"),
        OrderDate = order.Field<DateTime>("OrderDate"),
        SalesOrderNumber = order.Field<string>("SalesOrderNumber")
    };

foreach (var onlineOrder in query)
{
    Console.WriteLine("Order ID: {0} Order date: {1:d} Order number: {2}",
        onlineOrder.SalesOrderID,
        onlineOrder.OrderDate,
        onlineOrder.SalesOrderNumber);
}

More examples located at : http://msdn.microsoft.com/en-us/library/bb387004.aspx


There is also a 3rd party library that allows nearly complete SQL querying on datasets. It's not free though.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜