Linq学习笔记一(第一个ORM映射,熟悉一下Linq to sql语法)
1.在vs08下新建一个普通的网站项目。
右击网站项目->选择添加新项,然后选择“Linq to sql Classes”,命名为Northwind
这个文件.dbml是元数据定义文件。把数据库中的表与linq中的表关联起来。
2.点“服务器资源管理器”添加一个连接,连接到Northwind
打开表,把Customers表拖到Northwind.dbml中。
在Northwind.designer.cs中生成类似下面的代码:
[Table(Name="dbo.Customers")]
public partial class Customers : INotifyPropertyChanging, INotifyPropertyChanged
{}
即创建表Customers对应的映射。
3.创建了表对应的映射,对数据表的增删改查,就可以像NHibernate那样。
或者类似建立了表对应的Model,把普通的执行sql语句变成表对象操作。
避免写繁琐的sql语句。算是官方的ORM(Object/Relation Mapping 对象-关系数据库映射)吧。
挺方便,以前自己写的Model层,Dal层都省略了。
先看一下查询吧。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Common;
using System.Collections.Generic;
namespace FirstLinq
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Linq1();
Linq2();
}
/// <summary>
/// 熟悉一下linq语法,变量的定义类似js,无须声明类型。
/// </summary>
private void Linq1()
{
var age = 29;
var username = "greatverve";
var userlist = new[] { "a", "b", "c" };
foreach (var user in userlist)
{
Response.Write(user);
}
}
/// <summary>
/// 熟悉Linq to sql语法
/// </summary>
private void Linq2()
{
NorthwindDataContext ctx = new NorthwindDataContext("Data Source=ZHANGSHUQIANG;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa");
Customers customer = new Customers();
//执行普通的sql语句,查询CustomerID="ANATR"的记录
IEnumerable<Customers> customers = ctx.ExecuteQuery<Customers>("select * from Customers where CustomerID='ANATR'");
customer = customers.First();
Response.Write(customer.CustomerID);
//使用Linq查询单条记录
var cus = from c in ctx.Customers where c.CustomerID.Equals("ANATR") select c;
customer = cus.First();
Response.Write(customer.CompanyName);
//查询结果集,语法:from 临时表名 in 表集合 orderby 临时表名.字段名 升级序 select 临时表名
gdvCustomers.DataSource = from cust in ctx.Customers where cust.CustomerID != "ANATR" orderby cust.CompanyName descending select cust;
gdvCustomers.DataBind();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Common;
using System.Collections.Generic;
namespace FirstLinq
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Linq1();
Linq2();
}
/// <summary>
/// 熟悉一下linq语法,变量的定义类似js,无须声明类型。
/// </summary>
private void Linq1()
{
var age = 29;
var username = "greatverve";
var userlist = new[] { "a", "b", "c" };
foreach (var user in userlist)
{
Response.Write(user);
}
}
/// <summary>
/// 熟悉Linq to sql语法
/// </summary>
private void Linq2()
{
NorthwindDataContext ctx = new NorthwindDataContext("Data Source=ZHANGSHUQIANG;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa");
Customers customer = new Customers();
//执行普通的sql语句,查询CustomerID="ANATR"的记录
IEnumerable<Customers> customers = ctx.ExecuteQuery<Customers>("select * from Customers where CustomerID='ANATR'");
customer = customers.First();
Response.Write(customer.CustomerID);
//使用Linq查询单条记录
var cus = from c in ctx.Customers where c.CustomerID.Equals("ANATR") select c;
customer = cus.First();
Response.Write(customer.CompanyName);
//查询结果集,语法:from 临时表名 in 表集合 orderby 临时表名.字段名 升级序 select 临时表名
gdvCustomers.DataSource = from cust in ctx.Customers where cust.CustomerID != "ANATR" orderby cust.CompanyName descending select cust;
gdvCustomers.DataBind();
}
}
}
前台:
代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FirstLinq._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gdvCustomers" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gdvCustomers" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
真的是好便捷,普通的小项目完全够了,以后慢慢深入学习吧。
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。