参考自 原文:http://www.cnblogs.com/CaiNiaoZJ/category/334658.html
Linq对象
DataContext对象
//DataContext第一种构造方法
string str_conn ="Data Source=localhost;Initial Catalog=DB_Student;User ID=sa;Password=king";
DataContext dc=new DataContext(str_conn);
Table<Student> students = dc.GetTable<Student>(); //获取到表信息 这里是一个table的泛型类
dc.Dispose();
//IDbconnection conn=new sqlconnection("Data Source=localhost;Initial Catalog=DB_Student;User ID=sa;Password=xx");
//Datacontext ctx =new Datqacontext(conn);
//DataContext第二种构造方法
System.Data.SqlClient.SqlConnection conn =new System.Data.SqlClient.SqlConnection(str_conn);
DataContext dc1 =new DataContext(conn);
Table<Course> courses = dc1.GetTable<Course>();
dc1.Dispose();
//DataContext第三种构造方法
XmlMappingSource map = XmlMappingSource.FromXml(System.IO.File.ReadAllText(@"Mapping.xml"));
DataContext dc2 =new DataContext(str_conn, map);
Table<Student_Course> scs = dc2.GetTable<Student_Course>();
dc2.Dispose();
//DataContext第四种构造方法
DataContext dc3 =new DataContext(conn, map);
Table<Student> students1 = dc3.GetTable<Student>();
dc3.Dispose();
2.DataContext成员方法:
(1)方法SubmitChanges:
SubmitChanges方法用于把程序中对实体类对象的更改信息保存到数据库中,在该方法调用之前,DataContext对象会缓存程序中对实体类对象的更改信息,如果最后不执行方法SubmitChanges,那么这些修改的信息就不会保存到数据库中,该方法有两个方法原型:
①void SubmitChanges();
②void SubmitChanges(ConflictMode failureMode);
插入实例
1,单表插入
string str_sql ="Data Source=localhost;Initial Catalog=DB_Student;User ID=sa;Password=king";
2 DataContext dc =new DataContext(str_sql);
3
4 //实例化实体类对象
5 Student newstudent =new Student()
6 {
7 No ="20030012",
8 Name ="古筝",
9 Dept ="表演系",
10 Age =23,
11 Sex ="女"
12 };
13
14 //将对象添加到Table<T>
15 dc.GetTable<Student>().InsertOnSubmit(newstudent);
16
17 //提交
18 dc.SubmitChanges();
2,关系插入
一对多关系插入:
1 string str_sql ="Data Source=localhost;Initial Catalog=DB_Student;User ID=sa;Password=king";
2 DataContext dc =new DataContext(str_sql);
3
4 //实例化实体类对象
5 Course newcourse =new Course()
6 {
7 No ="ky030011",
8 Name ="声乐与语言发音",
9 Credit=5,
10 };
11
12 var student=dc.GetTable<Student>().Where(s=>s.No=="20030012").Single();
13 //实例化实体类对象
14 Student_Course sc =new Student_Course()
15 {
16 Cno = newcourse.No,
17 Sno = student.No,
18 Grade =95,
19 };
20
21 newcourse.Students.Add(sc);
22
23 //将对象添加到Table<T>
24 dc.GetTable<Course>().InsertOnSubmit(newcourse);
25
26 //提交
27 dc.SubmitChanges();
=======
4.删除操作:
LINQ TO SQL中的删除操作和插入操作类似,可以在DataContext对象的方法GetTable的返回值Table<T>,基础上调用DeleteOnSumbit方法,它允许将一个或多个实体类对象从Table<T>集合中被删除。下面是删除操作的示例:
i.删除操作:
删除操作
1 string str_sql ="Data Source=localhost;Initial Catalog=DB_Student;User ID=sa;Password=king";
2 DataContext dc =new DataContext(str_sql);
3
4 Console.WriteLine("学生:");
5 var student2 = dc.GetTable<Student>().Where(s => s.No =="20030012").Single();
6 Console.WriteLine(student2.ToString());
7
8 Console.WriteLine("选课:");
9 var courses = dc.GetTable<Student_Course>().Where(s => s.Sno =="20030012");
10 foreach (var c in courses)
11 {
12 Console.WriteLine(c.Course.ToString());
13 }
14
15 dc.GetTable<Student_Course>().DeleteAllOnSubmit(courses);
16 dc.GetTable<Student>().DeleteOnSubmit(student2);
17
18 dc.SubmitChanges();
19
20 dc.Refresh(RefreshMode.OverwriteCurrentValues,courses);
21 Console.WriteLine("删除操作后,学号为20030012的学生选课情况:");
22 foreach (var c in courses)
23 {
24 Console.WriteLine(c.Course.ToString());
25 }
26
27 Console.Read();
更新操作:
同样的更新操作也十分简单,修改实体类对象的属性,然后调用SubmitChanges方法,提交更改。但要注意的是在LINQ TO SQL中对主键的修改是不允许的,下面是关于更新操作的示例:
i.更新操作:
1 string str_sql ="Data Source=localhost;Initial Catalog=DB_Student;User ID=sa;Password=king";
2 DataContext dc =new DataContext(str_sql);
3
4 var students = dc.GetTable<Student>();
5
6 Console.WriteLine("修改前:");
7 foreach (var stu in students.Take(3))//从第一个元素开始,获取三个
Linq中Take、TakeWhile、Skip、SkipWhile的比较
8 {
9 Console.WriteLine(stu.ToString());
10 }
11
12 foreach(var stu in students)
13 {
14 stu.Age +=1;
15 }
16
17 dc.SubmitChanges();
18
19 dc.Refresh(RefreshMode.OverwriteCurrentValues, students);
Refresh方法用于根据数据库中的数据更新内存中的实体类对象的状态,在多用户并发访问数据库时,通过该方法可以获取数据库中最新的数据。
20
21 Console.WriteLine("修改后:");
22 foreach (var stu in students.Take(3))
23 {
24 Console.WriteLine(stu.ToString());
25 }
26
27 Console.WriteLine("\n");
28
29 Console.Read();