配置LINQ TO SQL
首先添加一个Linq to sql文件,以.dbml结尾的文件。无法把表拖拽到.dbml文件中,提示“所选对象使用不支持的数据提供程序”
解决方案
在服务器资源管理器中右键单击连接,选择"修改连接",将数据源由"用于 OLE DB 的 .NET Framework 数据提供程序"
修改为"用于 SQL Server 的 .NET Framework 数据提供程序"即可:
var result = from c in db.Categorys select c 被编译器编译为:
Table<Category> table = db.GetTable<Category>();
var result = from c in table select c;
示例:
添加一个实体类Student.cs
添加命名空间:using System.Data.Linq.Mapping;
1 [Table(Name = "Student")] 2 public class Student 3 { 4 private int stu_Id; 5 [Column(IsPrimaryKey=true)] 6 public int Stu_Id 7 { 8 get { return stu_Id; } 9 set { stu_Id = value; } 10 } 11 private string stu_Name; 12 [Column] 13 public string Stu_Name 14 { 15 get { return stu_Name; } 16 set { stu_Name = value; } 17 } 18 private int cla_Id; 19 [Column] 20 public int Cla_Id 21 { 22 get { return cla_Id; } 23 set { cla_Id = value; } 24 } 25 }
添加WebForm1.aspx
添加命名空间:using System.Data.Linq;
protected void Page_Load(object sender, EventArgs e) { DataContext db = new DataContext("Data Source=.;Initial Catalog=Test;Integrated Security=SSPI;"); Table<Student> student= db.GetTable<Student>(); //var result = student.Where(s => s.Stu_Name == "zhangsan").Select(s => s); var result = student.Select(s => s); foreach (Student stu in result) { Response.Write("学号:" + stu.Stu_Id + " -- " + "姓名:" + stu.Stu_Name +"</br>"); } }
显示:
二、连接数据库不在使用DataContext,而是创建一个类MSPetShop,然后继承DataContext
public MSPetShop(string connection) :base(connection);
三、表实体与数据库表映射
这是 Category实体类
这是Product实体类
3.1 在实体类中添加映射关系
这是Category实体类
这是Product实体类
3.2 两表联合查询
3.3 添加或修改数据
上图为修改数据
上图增加新增数据
示例:
创建一个实体 Class.cs 文件
1 [Table(Name = "Class")] 2 public class Class 3 { 4 private int class_Id; //班级号 5 [Column(IsPrimaryKey=true)] 6 public int Class_Id 7 { 8 get { return class_Id; } 9 set { class_Id = value; } 10 } 11 private string class_Name; //班级名称 12 [Column] 13 public string Class_Name 14 { 15 get { return class_Name; } 16 set { class_Name = value; } 17 } 18 19 private EntitySet<Student> students; 20 [Association(Storage = "students",OtherKey = "Class_Id")] 21 public EntitySet<Student> Students 22 { 23 get { return this.students; } 24 set { this.students.Assign(value); } 25 } 26 }
添加实体类Student.cs
1 [Table(Name = "Student")] 2 public class Student 3 { 4 private int stu_Id; //学号 5 [Column(IsPrimaryKey=true)] 6 public int Stu_Id 7 { 8 get { return stu_Id; } 9 set { stu_Id = value; } 10 } 11 private string stu_Name; //学生姓名 12 [Column] 13 public string Stu_Name 14 { 15 get { return stu_Name; } 16 set { stu_Name = value; } 17 } 18 private int cla_Id; //班级号(外键) 19 [Column] 20 public int Cla_Id 21 { 22 get { return cla_Id; } 23 set { cla_Id = value; } 24 } 25 26 private EntityRef<Class> classes; 27 [Association(Storage = "classes",ThisKey="Class_Id")] 28 public Class Classes 29 { 30 get { return this.classes.Entity; } 31 set { this.classes.Entity = value; } 32 } 33 }
添加Linq to sql文件,以.dbml结尾的文件
添加一个类MyDBContext.cs文件
1 public class MyDBContext :DataContext 2 { 3 public Table<Class> Class; 4 public Table<Student> Student; 5 public MyDBContext(string connection) : base(connection) { } 6 }
添加WebForm1.aspx
1 protected void Page_Load(object sender, EventArgs e) 2 { 3 MyDBContext db = new MyDBContext("Data Source=.;Initial Catalog=Test;Integrated Security=SSPI;"); 4 var result = from c in db.Student 5 from s in c.Class 6 select s; 7 8 foreach (var item in result) 9 { 10 Response.Write("学号:" + item.Student.Stu_Id + " -- " + "姓名:" + item.Student.Stu_Name + " -- " + "班级:" + item.Class_Name + "</br>"); 11 } 12 }
结果: