随便:LINQ OVER DataSet和ADO.NET的简单例子
2012-03-12 22:50
295 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Data.Linq;
namespace DataRelationExample
{
//使用LINQ OVER DataSet和ADO.NET
class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(@"Data Source=WENGJIXI;" +
@"Initial Catalog=NorthWind;Integrated Security=true;");
thisConnection.Open();
DataSet thisDataSet = new DataSet();
SqlDataAdapter cust = new SqlDataAdapter("select * from Customers", thisConnection);
SqlDataAdapter order = new SqlDataAdapter("select * from Orders", thisConnection);
cust.Fill(thisDataSet, "Customers");
order.Fill(thisDataSet, "Orders");
DataRelation custOrderRel = thisDataSet.Relations.Add("CustOrder", thisDataSet.Tables["Customers"].Columns["CustomerID"], thisDataSet.Tables["Orders"].Columns["CustomerID"]);
//返回泛型类型为DataRow,此方法返回的泛型类型可以用于LINQ表达式或查询上,如下面的LINQ查询。
var customers = thisDataSet.Tables["Customers"].AsEnumerable();
var orders = thisDataSet.Tables["Orders"].AsEnumerable();
//使用LINQ OVER DataSet方法
var preferedCustomers = from c in customers
where c.GetChildRows("CustOrder").Length > 10//DataRow的GetChildRows()方法,返回此行相关联的子行。在此是关联到Orders表上。
orderby c.GetChildRows("CustOrder").Length
select c;
//循环输出每一行
foreach (var item in preferedCustomers)
{
Console.WriteLine("{0} orders: {1} {2}, {3} {4} ", item.GetChildRows("CustOrder").Length, item["CustomerID"], item["CompanyName"], item["City"], item["Region"]);
}
thisConnection.Close();
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Data.Linq;
namespace DataRelationExample
{
//使用LINQ OVER DataSet和ADO.NET
class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(@"Data Source=WENGJIXI;" +
@"Initial Catalog=NorthWind;Integrated Security=true;");
thisConnection.Open();
DataSet thisDataSet = new DataSet();
SqlDataAdapter cust = new SqlDataAdapter("select * from Customers", thisConnection);
SqlDataAdapter order = new SqlDataAdapter("select * from Orders", thisConnection);
cust.Fill(thisDataSet, "Customers");
order.Fill(thisDataSet, "Orders");
DataRelation custOrderRel = thisDataSet.Relations.Add("CustOrder", thisDataSet.Tables["Customers"].Columns["CustomerID"], thisDataSet.Tables["Orders"].Columns["CustomerID"]);
//返回泛型类型为DataRow,此方法返回的泛型类型可以用于LINQ表达式或查询上,如下面的LINQ查询。
var customers = thisDataSet.Tables["Customers"].AsEnumerable();
var orders = thisDataSet.Tables["Orders"].AsEnumerable();
//使用LINQ OVER DataSet方法
var preferedCustomers = from c in customers
where c.GetChildRows("CustOrder").Length > 10//DataRow的GetChildRows()方法,返回此行相关联的子行。在此是关联到Orders表上。
orderby c.GetChildRows("CustOrder").Length
select c;
//循环输出每一行
foreach (var item in preferedCustomers)
{
Console.WriteLine("{0} orders: {1} {2}, {3} {4} ", item.GetChildRows("CustOrder").Length, item["CustomerID"], item["CompanyName"], item["City"], item["Region"]);
}
thisConnection.Close();
Console.ReadLine();
}
}
}
相关文章推荐
- ado.net和linq over dataset 笔记(1)
- [Linq] Linq Over DataSet[Ado.net]
- ado.net连接数据库简单例子
- ADO.Net 类型化DataSet的简单介绍
- ADO.NET 中的DATASet对象应用例子
- ADO.Net 类型化DataSet的简单介绍
- ADO.NET Entity Framework 一个简单数据绑定例子
- C# 2008 学习笔记 - LINQ to ADO.NET(二)- LINQ to DataSet
- ADO.NET Entity Framework 一个简单数据绑定例子
- ADO.NET Entity Framework 一个简单数据绑定例子
- 浅谈ADO.NET中的对象——Connection、Command、DataReader、DataAdapter、DataSet、DataTable
- 简单快速开发C\S架构程序用最简单的不分层最快的效率达到功能要求的例子程序FrmCommnets
- ADO.Net Entity Framework Linq To Entities 语法功能汇编
- ADO.NET Entity Framework Extensions 简单应用
- 编码实现>微软数据访问技术ADO.NET>使用DataSet>
- 分层架构、MVC、ViewState、ADO.NET、DataReader和DataSet
- ADO.NET Entity Framework Extensions 简单应用
- ADO.NET学习之SqlDataAdapter,DataSet
- ADO.NET常用对象详解之:DataSet对象
- o/r mapping in ado.net using DataSet