易截截图软件、单文件、免安装、纯绿色、仅160KB

.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交叉表查询 如何实现

请教大家一个有关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 ......

循序渐进讲解SQL查询语句高级应用技巧

一、 简单查询
简单的Transact-SQL查询只包括选择列表、from子句和WHERE子句。它们分别说明所查询列、查询的
表或视图、以及搜索条件等。
例如,下面的语句查询testtable表中姓名为“张三”的nickname字段和email字段。
SELECT nickname,email
from testtable
WHERE name='张三'
 
(一) 选择 ......

sql server 定义主键

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 ......

【SQL SERVER中一些特别地方的特别解法2】


/*----------------------------------------------------------------
-- Author  :feixianxxx(poofly)
-- Date    :2010-04-20 20:10:41
-- Version:
--      Microsoft SQL Server 2008 (SP1) -  ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号