教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 学前教育 >

一步一步学Linq to sql(2)

来源:网络收集 时间:2026-09-07
导读: { [Column(IsPrimaryKey = true)] public string CustomerID {get; set;} [Column(Name = \)] public string Name { get; set; } [Column] public string City {get; set;} } 以Northwind数据库为例,上述Customers

{ [Column(IsPrimaryKey = true)] public string CustomerID {get; set;} [Column(Name = \)] public string Name { get; set; } [Column] public string City {get; set;} } 以Northwind数据库为例,上述Customers类被映射成一个表,对应数据库中的 Customers表。然后在类型中定义了

三个属性,对应表中的三个字段。其中,CustomerID字段是主键,如果没有指定Column特性的Name属性,那么系统会把属性名作为数据表的字段名,也就是说实体类的属性名就需要和数据表中的字段名一致。

现在,创建一个ASP.NET页面,然后在页面上加入一个GridView控件,使用下面的代码进行绑定数据:

using System.Data.Linq; DataContext ctx = new DataContext(\); Table Customers = ctx.GetTable(); GridView1.DataSource = from c in Customers where c.CustomerID.StartsWith(\) select new {顾客ID=c.CustomerID, 顾客名=c.Name, 城市=c.City}; GridView1.DataBind(); 使用DataContext类型把实体类和数据库中的数据进行关联。你可以直接在DataContext的构造方法中定义连接字符

串,也可以使用IDbConnection:

using System.Data.SqlClient; IDbConnection conn = new SqlConnection(\); DataContext ctx = new DataContext(conn); 之后,通过GetTable获取表示底层数据表的Table类型,显然,数据库中的Customers表的实体是Customer类型。

随后的查询句法,即使你不懂SQL应该也能看明白。从Customers表中找出CustomerID以“A”开头的记录,并把CustomersID、Name以及City封装成新的匿名类型进行返回。

结果如下图:

强类型DataContext

public partial class NorthwindDataContext : DataContext { public Table Customers; public NorthwindDataContext(IDbConnection connection) : base(connection) { } public NorthwindDataContext(string connection) : base(connection) { } } 强类型数据上下文使代码更简洁:

NorthwindDataContext ctx = new NorthwindDataContext(\); GridView1.DataSource = from c in ctx.Customers where c.CustomerID.StartsWith(\) select new { 顾客ID = c.CustomerID, 顾客名 = c.Name, 城市 = c.City }; GridView1.DataBind(); DataContext其实封装了很多实用的功能,下面介绍。

日志功能

using System.IO; NorthwindDataContext ctx = new NorthwindDataContext(\); StreamWriter sw = new StreamWriter(Server.MapPath(\), true); // Append ctx.Log = sw; GridView1.DataSource = from c in ctx.Customers where c.CustomerID.StartsWith(\) select new { 顾客ID = c.CustomerID, 顾客名 = c.Name, 城市 = c.City }; GridView1.DataBind(); sw.Close(); 运行程序后在网站所在目录生成了log.txt,每次查询都会把诸如下面的日志追加到文本文件中:

SELECT [t0].[CustomerID], [t0].[ContactName], [t0].[City] FROM [Customers] AS [t0] WHERE [t0].[CustomerID] LIKE @p0 -- @p0: Input String (Size = 2; Prec = 0; Scale = 0) [A%] -- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.20706.1 应该说这样的日志对于调试程序是非常有帮助的。

探究查询

using System.Data.Common; using System.Collections.Generic; NorthwindDataContext ctx = new NorthwindDataContext(\); var select = from c in ctx.Customers where c.CustomerID.StartsWith(\) select new { 顾客ID = c.CustomerID, 顾客名 = c.Name, 城市 = c.City }; DbCommand cmd = ctx.GetCommand(select); Response.Write(cmd.CommandText + \); foreach (DbParameter parm in cmd.Parameters) Response.Write(string.Format(\参数名:{0},参数值:{1}
\, parm.ParameterName, parm.Value)); Customer customer = ctx.Customers.First(); customer.Name = \; IList queryText = ctx.GetChangeSet().ModifiedEntities; Response.Write(((Customer)queryText[0]).Name); 在这里,我们通过DataContext的GetCommand方法获取了查询对应的DbCommand,并且输出了CommandText和所

有的DbParameter。之后,我们又通过GetChangeSet方法获取了修改后的实体,并输出了修改内容。 执行查询

NorthwindDataContext ctx = new NorthwindDataContext(\); string newcity = \; ctx.ExecuteCommand(\, newcity); IEnumerable customers = ctx.ExecuteQuery(\* from Customers where CustomerID like 'A%'\); GridView1.DataSource = customers; GridView1.DataBind(); 前一篇文章已经说了,虽然Linq to sql能实现90%以上的TSQL功能。但是不可否认,对于复杂的查询,使用TSQL

能获得更好的效率。因此,DataContext类型也提供了执行SQL语句的能力。代码的执行结果如下图:

创建数据库

testContext ctx = new testContext(\); ctx.CreateDatabase(); [Table(Name = \)] public class test { [Column(IsPrimaryKey = true, IsDbGenerated = true)] public int ID { get; set; } [Column(DbType=\)] public string Name { get; set; } } public partial class testContext : DataContext { public Table test; public testContext(string connection) : base(connection) { } } 这段代码在数据库中创建了名为testdb的数据库,等同于下面的脚本: …… 此处隐藏:1869字,全部文档内容请下载后查看。喜欢就下载吧 ……

一步一步学Linq to sql(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/596912.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)
  • 1、支付成功后,为何无法下载内容?
    付费后下载不了,请核对下微信账单信息,确保付费成功;已付费成功了还是下载不了,有可能是电脑的浏览器兼容性问题或者手机机型不支持的问题。
  • 2、付费后能否更换浏览器或者清理浏览器缓存后进行下载操作?
    更换浏览器或者清理浏览器缓存会导致下载不成功,请不要更换浏览器和清理浏览器缓存。
  • 3、如何联系客服?
    打开微信扫描下方二维码,请及时联系客服解决。
    QQ咨询:370150219 点击这里给我发消息,请把【付款记录详情】截图给客服,同时把您购买的文章【网址】发给客服。客服会在24小时内把文档发送给您。