lambda表达式自学笔记2
2011-06-27 13:22
483 查看
DataContext DataContext类型(数据上下文)功能:
1.以日志形式记录DataContext生成的SQL
2.执行SQL(包括查询和更新语句)
3.创建和删除数据库
DataContext是实体和数据库之间的桥梁。 定义实体类
Customer.cs
Default.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.Mapping; namespace DannyWeb
{
[Table(Name="Customers")]
public class Customer
{
[Column(IsPrimaryKey = true)] public string CustomerID { get; set; } [Column(Name = "ContactName")] public string Name { get; set; } [Column] public string City { get; set; } }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
Default.aspx.cs
显示结果: 注意:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Linq;
using DannyWeb;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataContext ctx = new DataContext("server=.;database=Northwind;uid=sa;pwd=");
Table<Customer> Customers = ctx.GetTable<Customer>();
GridView1.DataSource = from c in Customers
where c.CustomerID.StartsWith("A")
select new { 顾客ID = c.CustomerID, 顾客名 = c.Name, 城市 = c.City };
GridView1.DataBind();
}
}
要手动添加System.Data.Linq.Mapping引用。 还可以直接使用如下:
2011-6-2 11:27 danny
using System.Data.SqlClient;
...
IDbConnection conn = new SqlConnection("server=xxx;database=Northwind;uid=xxx;pwd=xxx"); DataContext ctx = new DataContext(conn);
...
相关文章推荐
- .net系统自学笔记——委托、Lambda表达式和事件
- python自学笔记(3)--python基本语句
- 【自学笔记】集合类概述(有点没理解清楚)
- Scala 自学笔记3_特质
- Python自学笔记(2)
- 自学笔记——shell(文本处理工具)
- 伟东山视频自学笔记——打补丁
- 自学之笔记总结
- 自学java基础笔记
- 自学JavaScript笔记
- DCT变换自学笔记
- Android开发自学笔记(Android Studio)—4.5 ProgressBar及其子类
- pixhawk自学笔记之uorb学习总结
- php自学笔记外传---部分内建函数
- 黑马程序员——.NET自学课程第三天笔记
- Delphi自学笔记之注释
- SAP ABAP基础知识自学笔记二
- Linux 自学笔记(一) 常见命令
- Oracle自学笔记2
- Linux自学笔记——linux进程及作业管理