.NETѧϰÊÖ¼ÇÖ®£ºlinq to SQL(¶þ)
ÔÚVisual Studio 2008 ÖÐʹÓÃO/RÉè¼ÆÆ÷£º
µãÌí¼ÓÏîÄ¿£¬Ñ¡Ôñ´´½¨Linq to SQLÏîÄ¿£¬Ê¹Ó÷þÎñÆ÷×ÊÔ´¹ÜÀíÆ÷Á¬½ÓNorthwindÊý¾Ý¿â£¬½«CustomersºÍOrdersÁ½¸ö±íÍϵ½Éè¼Æ½çÃæÉÏ£¬ÏµÍ³»á×Ô¶¯´´½¨app.configºÍNorthwid.designer.cs,Ç°ÕßÊÇÅäÖÃÁ¬½ÓÊý¾Ý¿âµÄÁ¬½Ó×Ö´®£»ºóÕß»áÉú³ÉÒ»¸ö¼Ì³Ð×ÔDataContextµÄÀࣺNorthwindDataContext¡£
ʹÓÃlinqµ÷³öÊý¾Ý£º
static void Main(string[] args)
{
NorthwindDataContext dc= new NorthwindDataContext();
dc.Log=Console.Out;
var query=from c in dc.Customers
join o in dc.Orders on c.CustomerID equals o.CustomerID
orderby c.CustomerID
select new {
c.CustomeriD,c.CompanyName,c.Country,o.OrderID,o.OrderDate};
foreach(var item in query)
{
Console.WriteLine(item.CustomerID+"|"+item.CompanyName
+"|"+item.Country+"|"+item.OrderID
+"|"+item.OrderDate);
}
}
ʹÓô洢¹ý³Ì£º
½«´æ´¢¹ý³ÌÍϵ½O/RÉè¼ÆÆ÷µÄÓÒ²àÇøÓò¡£
static void Main(string[] args)
{
NorthwindDataContext dc= new NorthwindDataContext();
ISingleResult<Ten_Most_Expensive_ProductsResult> result=
dc.Ten_Most_Expensive_Products();
foreach(Ten_Most_Expensive_ProductsResult item in result)
{
Console.WriteLine(item.TenMostExpensiveProducts+"|"+
item.UnitPrice);
}
}
Ïà¹ØÎĵµ£º
Çë½Ì´ó¼ÒÒ»¸öÓйØSQL½»×¤±¨±í²éѯÎÊÌ⣬»¶Ó¸÷λָ½Ì£¡
ÎÒÏë°Ñͼ1µÄʹÓÃÐÅÏ¢£¬Ê¹ÓÃSQLÓï¾ä£¬ÊµÏÖÈçͼ2µÄ½á¹û¡£
±íÃû
ÐòºÅ
×Ö¶ÎÃû
a
1
c
a
2
d
a
3
e
a
4
f
a
5
g
b
1
h
b
2
i
b
3
j
b
4
k
b
5
l
c
1
m
c
2
n
c
3
o
c
4
p
c
5
q
ͼ1
±íÃû
ÐòºÅ
1
2
3
4
5
......
Ò»¡¢Ê¹ÓÃSqlConnection¶ÔÏóÁ¬½ÓSQL ServerÊý¾Ý¿â
1¡¢µÇ¼Êý¾Ý¿âµÄ·½Ê½ÎªSQL ServerÑé֤ģʽ
server=·þÎñÆ÷Ãû;database=Êý¾Ý¿âÃû³Æ;uid=Óû§;pwd=ÃÜÂë
2¡¢µÇ¼Êý¾Ý¿âµÄ·½Ê½ÎªWindowsÑé֤ģʽ
server=·þÎñÆ÷Ãû;database=Êý¾Ý¿âÃû³Æ;Integrated Security=SSPI
ÆäÖУ¬Integrated Security¼´Trusted_Connection,È¡Ö ......
drop table father;
create table father(
id int identity(1,1) primary key,
name varchar(20) not null,
age int not null
)
drop table mother;
create table mother(
id int identity(1,1),
name varchar(20) not null,
age int not null,
husban ......
/*----------------------------------------------------------------
-- Author :feixianxxx(poofly)
-- Date :2010-04-20 20:10:41
-- Version:
-- Microsoft SQL Server 2008 (SP1) - ......