手把手教你用好LINQ to SQL

时尚2025-11-05 04:48:1254
手把手教你用好LINQ to SQL
复制 using System; using System.Collections.Generic; using System.Data.Linq; using System.Linq; using System.Text; using LinqToSqlDemo.Orm; namespace LinqToSqlDemo.Test { class Program { private static DataClassesDataContext dataContext = new DataClassesDataContext(); private static void Output() { //输出分类信息 foreach (Category c in dataContext.Categories) { Console.WriteLine("分类" + c.ID + ":" + c.Name); } //输出体育新闻下的手把手教公告信息 Category categorySport = dataContext.Categories.Single(c => c.Name == "体育新闻"); foreach (Bulletin b in categorySport.Bulletins) { Console.WriteLine("标题:" + b.Title); Console.WriteLine("内容:" + b.Content); Console.WriteLine("发布日期:" + b.Date); Console.WriteLine("所属分类:" + b.Category1.Name); } } private static void TestInsert() { //生成分类实体类 Category category1 = new Category() { Name = "国际要闻" }; Category category2 = new Category() { Name = "体育新闻" }; Category category3 = new Category() { Name = "财经快报" }; //生成公告实体类 Bulletin bulletin1 = new Bulletin() { Content = "曼联晋级冠军杯四强", Date = DateTime.Now, Title = "曼联晋级冠军杯四强" }; Bulletin bulletin2 = new Bulletin() { Content = "18:00直播亚冠首尔VS山东,敬请期待!免费信息发布网用好!手把手教WordPress模板用好",手把手教 Date = DateTime.Now, Title = "18:00直播亚冠首尔VS山东" }; //将公告加入相应分类 category2.Bulletins.Add(bulletin1); category2.Bulletins.Add(bulletin2); //加入数据库 dataContext.Categories.InsertOnSubmit(category1); dataContext.Categories.InsertOnSubmit(category2); dataContext.Categories.InsertOnSubmit(category3); dataContext.SubmitChanges(); } private static void TestDelete() { dataContext.Categories.DeleteOnSubmit(dataContext.Categories.Single(c => c.Name == "国际要闻")); dataContext.SubmitChanges(); } private static void TestUpdate() { Category categoryFinance = dataContext.Categories.Single(c => c.Name == "财经快报"); categoryFinance.Name = "财经新闻"; dataContext.SubmitChanges(); } static void Main(string[] args) { Console.WriteLine("==============================Linq to SQL 测试=============================="); Console.WriteLine(); Console.WriteLine("==============================测试Insert=============================="); Console.WriteLine(); TestInsert(); Output(); Console.WriteLine("==============================测试Delete=============================="); Console.WriteLine(); TestDelete(); Output(); Console.WriteLine("==============================测试Update=============================="); Console.WriteLine(); TestUpdate(); Output(); Console.ReadLine(); } } } 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.
本文地址:http://www.bzve.cn/html/348e1999632.html
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

全站热门

用罗技G402玩英雄联盟——打造极致游戏体验(解密G402在英雄联盟中的优势与应用技巧)

SCSS 中这些技巧,你可能还不知道!

MySQL索引原理与应用:索引类型,存储结构与锁

简单实现MySQL数据库的日志审计

打造网红电脑钟表的制作教程(用创意与技术,打造属于自己的网红电脑钟表)

大规模升级来临,谈谈Oracle 12cR2使用经验

数据库中存储过程和触发器有什么区别

分享一份实用的MySQL数据库巡检脚本,值得收藏

友情链接

滇ICP备2023006006号-39