标签:des style blog ar io color sp for on
ORM(O—Object对象,R—Relation关系,M—Mapping映射)
表名——类名
列名——属性名
表的关系——类的成员对象
LinQ——集成化查询语言 SQL——结构化查询语言
LinQ包括:LinQ to SQL、LinQ to Object、LinQ to DataSet、LinQ to Entity
LinQ to SQL
第一步:建立LinQ to SQL类(项目右键→“添加”→“类”→“数据”中的“LINQ to SQL类”→在服务器资源管理器中添加数据库连接→找到需要的表,拖动到页面)
第二步:实例化上下文对象。
第三步:操作
//实例化上下文 MyDBDataContext context = new MyDBDataContext(); //1、造对象 Info data = new Info(); data.Code = "p211"; data.Name = "周青"; data.Nation = "N001"; data.Sex = false; data.Birthday = new DateTime(1990, 1, 1); //2、在上下文中注册上面新造的对象 context.Info.InsertOnSubmit(data); //3、将数据提交到数据库 context.SubmitChanges();
MyDBDataContext context = new MyDBDataContext(); //1、在Info表中查询Code为“p003”的数据 var q = context.Info.Where(p => p.Code == "p003"); //var q是动态推导类型,会自动推导数据类型。(p => p.Code == "p003")意思:将p中数据传入(p.Code == "p003")进行判断 if (q.Count() > 0) { Info data = q.First(); //2、注册 context.Work.DeleteAllOnSubmit(data.Work); context.Family.DeleteAllOnSubmit(data.Family); context.Info.DeleteOnSubmit(data); //3、提交给数据库 context.SubmitChanges(); }
static void Main(string[] args) { MyDBDataContext context = new MyDBDataContext(); //1、在Info表中查找Code为“p001”的数据,放入内存 var q = context.Info.Where(p => p.Code == "p001"); if (q.Count() > 0) { Info data = q.First(); //2、在内存中修改数据 data.Name = "胡生"; data.Nation = "n001"; data.Sex = false; //3、将修改后的数据提交给数据库 context.SubmitChanges(); } }
static void Main(string[] args) { MyDBDataContext context = new MyDBDataContext(); //1、查询Info表中所有数据 var q = context.Info; //2、显示 foreach(Info data in q) { //data.Nation1:当前人员对应的民族对象。 Console.WriteLine(data.Name+"\t"+data.Nation1.Name); //data.Work:当前人员对应的工作记录集合 foreach (Work work in data.Work) { Console.WriteLine("\t"+work.Firm+"\t"+work.Depart); } } }
var q = context.Info.Where(p => p.Code == "p211"); //默认返回的是集合 if (q.Count() > 0) //看看集合中是否查出数据来了 { Info data = q.First(); //取第一个对象出来 Console.WriteLine(data.Nation1.Name + data.Name); }
MyDBDataContext context = new MyDBDataContext(); //查询所有 var q = from p in context.Info select p; var q = context.Info; //单条件查询 var q = from p in context.Info where p.Code == "p003" select p; var q = context.Info.Where(p => p.Code == "p003"); //多条件查询 var q = from p in context.Car where p.Price > 30 && p.Brand == "b002" select p; var q = context.Car.Where(p => p.Price > 30 && p.Brand == "b002"); //效果同下一行 var q = context.Car.Where(p => p.Price > 30).Where(p => p.Brand == "b002"); //Lambda表达式(最最简化的函数) var q = from p in context.Car where p.Price > 30 || p.Brand == "b002" select p; var q = context.Car.Where(p => p.Price > 30 || p.Brand == "b002"); //模糊查询 var q = from p in context.Car where p.Name.Contains("5") select p; //包含 var q = from p in context.Car where p.Name.StartsWith("奥迪") select p; //开头 var q = from p in context.Car where p.Name.EndsWith("奥迪") select p; //结尾 var q = context.Car.Where(p => p.Name.Contains("5")); //包含 var q = context.Car.Where(p => p.Name.StartsWith("奥迪")); //开头 var q = context.Car.Where(p => p.Name.EndsWith("型")); //结尾 var q = from p in context.Car where p.Name.Substring(2, 1) == "5" select p; //第三个字符 var q = context.Car.Where(p => p.Name.Substring(2, 1) == "5"); //第三个字符是5 //Distinct查询 var q = (from p in context.Car select p.Brand).Distinct(); //去重 var q = context.Car.Select(p => p.Brand).Distinct(); //去重 //连接查询——对象之间的关联关系点出来的。 var q = context.Car.Where(p => p.Brand1.Productor.Prod_Name == "一汽丰田"); //分页 var query = context.Car.Skip(2 * 4).Take(4); //排序 var query = context.Car.OrderBy(p => p.Price); //升序 var query = context.Car.OrderByDescending(p => p.Price); //降序 //集合操作 var query1 = context.Car.Where(p => p.Brand1.Productor.Prod_Name == "一汽丰田"); var query2 = context.Car.Where(p => p.Price > 30); //交集 var query = query1.Intersect(query2); //并集 var query = query1.Union(query2); //差集 var query = query1.Except(query2); //补集 var query = context.Car.Except(query1);
//LinQ2SQL MyDBDataContext context = new MyDBDataContext(); List<Car> list = context.Car.ToList(); //转成集合 //LinQ2Object ——对内存中的集合操作 var query = list.OrderBy(p => p.Price); //按价格排序
标签:des style blog ar io color sp for on
原文地址:http://www.cnblogs.com/phantom-k/p/4173030.html