.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);
}
}
相关文档:
一般用BCP在处理这个事情,但有时也需要一些特殊的处理,以下是生成表中的一些数据,带有where条件的选择生成数据,是我一个同事修改的,直接拿过来用了:
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
Create Proc proc_insert_where (@tablename varchar(256),@where varchar(256 ......
在2005中有同义词与复制的概念
同义词的主要作用是:
一:宿短对象的名称,减少工作人员书写的时间,提高效率。我们知道访问数据库一个对象的通常最全的对象名称是:服务器名称。数据库名称。架构名称。对象名称
二:同步数据。 ......
1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1 <>1
法二:select top 0 * into b from a
2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from b;
3、说明:跨数据库之间表的拷贝(具体数据使用 ......
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 ......