第一篇博客,还望各位大神勿喷

小弟在此代码奉上........

借用NorthWind数据库,实现一个商品展示的小功能。上代码:

添加对Linq的引用

  1. using System.Data.Linq;//添加对Linq的引用
  2. using System.Data.Linq.Mapping;//配置对象和映射关系的命名空间

由于图简单,所以写了很少的字段,继续代码

  1. [Table(Name = "Products")]
  2. public class Product
  3. {
  4. [Column(Name = "ProductID", DbType = "int identity(1,1) not null", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert, CanBeNull = true)]
  5. public int ProductID { get; set; }
  6. [Column(Name = "ProductName", DbType = "nvarchar(40) not null")]
  7. public string ProductName { get; set; }
  8. [Column(Name = "CategoryID", DbType = "int", CanBeNull = true)]
  9. public int CategoryID { get; set; }
  10. [Column(Name = "UnitPrice", DbType = "money")]
  11. public decimal? Price { get; set; }
  12. }
  1. [Table(Name = "Categories")]
  2. public class Categoty
  3. {
  4. [Column(Name = "CategoryID", DbType = "int identity(1,1) not null", IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = true, AutoSync = AutoSync.OnInsert)]
  5. public int ID { get; set; }
  6. [Column(Name = "CategoryName", DbType = "nvarchar(15) not null")]
  7. public string CategoryName { get; set; }
  8. [Column(Name = "Description", DbType = "ntext null")]
  9. public string Description { get; set; }
  10.  
  11. private EntitySet<Products> _products;
  12. [Association(Name = "C_P", IsForeignKey = false, OtherKey = "CategoryID", ThisKey = "ID", Storage = "_products")]
  13. public IList<Products> Products
  14. {
  15. get { return _products; } //上级接受下级参数,直接返回,上级:Category 下级:Product
  16. set { _products.Assign(value); }//下级接受上级参数,使用Assign方法进行向上转换
  17. }
  18. }

上面分别是两个类:商品类Product与商品类别Category,下面继续创建上下文对象

  1. public class NorthwindDBContext : DataContext
  2. {
  3. private static readonly string conn = ConfigurationManager.ConnectionStrings["Myconn"].ConnectionString;//数据库的连接
  4.  
  5. public NorthwindDBContext()
  6. : base(conn) { }
  7.  
  8. public NorthwindDBContext(string connection)
  9. : base(connection) { }
  10.  
  11. public Table<Categoty> Category;
  12.  
  13. public Table<Products> Products;
  14. }

接下来就是展示页面了,创建一个aspx页面吧,扔两个控件上去

  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <asp:DropDownList ID="DropDownListCategory" runat="server">
  5. </asp:DropDownList>
  6. <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
  7. <asp:GridView ID="GridView1" runat="server">
  8. </asp:GridView>
  9. </div>
  10. </form>
  11. </body>

页面现在也有了,后台代码:

  1. public partial class ProductsList : System.Web.UI.Page
  2. {
  3. Model.NorthwindDBContext db = new Model.NorthwindDBContext();
  4. protected void Page_Load(object sender, EventArgs e)
  5. {
  6. if (!IsPostBack)
  7. {
  8. var query = db.Products.Count();
  9. BandCategory();
  10. }
  11. }
  12.  
  13. private void BandCategory()
  14. {
  15.  
  16. var allCate = from c in db.Catrgorys select c;
  17. this.DropDownListCategory.DataValueField = "ID";
  18. this.DropDownListCategory.DataTextField = "Name";
  19. this.DropDownListCategory.DataSource = allCate;
  20. this.DropDownListCategory.DataBind();
  21. }
  22.  
  23. protected void Button1_Click(object sender, EventArgs e)
  24. {
  25. BandProduct();
  26. }
  27.  
  28. private void BandProduct()
  29. {
  30.  
  31. //生成日志文件,方便程序出错查看生成的SQL语句
  32. db.Log = new StreamWriter(Server.MapPath("~/log.txt"), true);
  33. int cid = int.Parse(this.DropDownListCategory.SelectedValue);
  34. List<Model.Product> lst = db.Products.ToList();
  35. var Products = db.Products.Where(x => x.CategoryID == cid);
  36. //拿到类别对象
  37. //var cate = db.Category.Where(x => x.CategoryID == cid).First();
  38. this.GridView1.DataSource = Products;//cate.Product;两种方法都行
  39. this.GridView1.DataBind();
  40. db.Log.Flush();
  41. db.Log.Close();
  42.  
  43. }

最后效果:

表示刚工作学生路过,大神勿喷...以后会陆续分享一些工作心得的

回顾:Linq To SQL语法 - 实体类的更多相关文章

  1. LINQ to SQL 建立实体类

    使用LINQ to SQL时,需要首先建立用于映射数据库对象的模型,也就是实体类.在运行时,LINQ to SQL 根据LINQ表达式或查询运算符生成SQL语句,发送到数据库进行操作.数据库返回后,L ...

  2. 步步学LINQ to SQL:为实体类添加关系【转】

    [IT168 专稿]本文详细为你阐述了如何在你的应用程序中实现LINQ to SQL.附件的示例程序包括了这里探讨的所有代码,还提供了一个简单的WPF图形界面程序来显示通过数据绑定返回的结果集. 第一 ...

  3. LINQ to SQL 建立实体类 (转)

    http://www.cnblogs.com/DebugLZQ/archive/2012/11/14/2770449.html 使用LINQ to SQL时,需要首先建立用于映射数据库对象的模型,也就 ...

  4. Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等)

    Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等) 子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 = from c i ...

  5. FreeSql (二十四)Linq To Sql 语法使用介绍

    原本不支持 IQueryable 主要出于使用习惯的考虑,如果继承 IQueryable,编写代码的智能总会提示出现一堆你不想使用的方法(对不起,我有强迫症),IQueryable 自身提供了一堆没法 ...

  6. [转]LINQ To SQL 语法及实例大全

    转载自:http://blog.csdn.net/pan_junbiao/article/details/7015633 LINQ to SQL语句(1)之Where Where操作 适用场景:实现过 ...

  7. .net 根据匿名类生成实体类,根据datatable生成实体类,根据sql生成实体类

    在开发中可能会遇到这几种情况 1.EF或LINQ查询出来的匿名对象在其它地方调用不方便,又懒的手动建实体类 2.通过datatable反射实体需要先建一个类 ,头痛 3.通过SQL语句返回的实体也需要 ...

  8. LINQ To SQL 语法及实例大全

    http://blog.csdn.net/pan_junbiao/article/details/7015633 http://blog.csdn.net/pan_junbiao/article/de ...

  9. Linq to sql语法

    LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子 ...

随机推荐

  1. Flume 1.4.0 User Guide

    Apache Flume is a distributed, reliable, and available system for efficiently collecting, aggregatin ...

  2. java+tomcat 在 linux下的部署

    一.配置JAVA运行环境 1.安装jdk. 从sun公司网站www.sun.com下载linux版本的jdk, 建议使用jdk1.6版本.地址http://java.sun.com/javase/do ...

  3. 配置mysql允许远程连接

    开启MySQL数据库的远程连接权限: use mysql://进入数据库 grant all privileges on *.* to root@'%'identified by '123465789 ...

  4. js获取url?后的参数

    function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new O ...

  5. Javascript 中神奇的 this

    Javascript 当中的 this 与其他语言是完全不同的机制,很有可能会让一些编写其他语言的工程师迷惑. 1. 误以为 this 指向函数自身 根据 this 的英语语法,很容易将函数中出现的  ...

  6. CentOS下安装nginx并且升级nginx到最新版

    yum install yum-fastestmirro yum install nginxyum update nginx(之前先配置一下)vim /etc/yum.repos.d/nginx.re ...

  7. 一个简单的javascript获取URL参数的代码

    function request(paras){ var url = location.href; var paraString = url.substring(url.indexOf("? ...

  8. Properties配置文件

    package file; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; ...

  9. Cstring类

    GetLength: 获取CString类的对象包含的字符串的长度(字节数) IsEmpty: 测试CString类的对象包含的字符串是否为空 Empty: 使CString类的对象包含的字符串为空字 ...

  10. Qt全局热键(windows篇)(使用RegisterHotKey和句柄进行注册)

    转载:http://www.cuteqt.com/blog/?p=2088 Qt对于系统底层,一直没有很好的支持,例如串口并口通信,还有我们经常都会用到的全局热键,等等.既然Qt可能出于某种原因,不对 ...