您的位置:首页 > 数据库

LINQ to SQL快速上手 step by step

2010-08-18 17:14 471 查看
原文链接:http://kb.cnblogs.com/page/70851/

 

 

 

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.Category)
            {
                Console.WriteLine("分类" + c.ID + ":" + c.Name);
            }

            //输出体育新闻下的公告信息
            Category categorySport = dataContext.Category.Single(c => c.Name == "体育新闻");
            foreach (Bulletin b in categorySport.Bulletin)
            {
                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山东,敬请期待!!!",
                Date = DateTime.Now,
                Title = "18:00直播亚冠首尔VS山东"
            };

            //将公告加入相应分类
            category2.Bulletin.Add(bulletin1);
            category2.Bulletin.Add(bulletin2);

            //加入数据库
            dataContext.Category.InsertOnSubmit(category1);
            dataContext.Category.InsertOnSubmit(category2);
            dataContext.Category.InsertOnSubmit(category3);
            dataContext.SubmitChanges();
        }

        private static void TestDelete()
        {
            dataContext.Category.DeleteOnSubmit(dataContext.Category.Single(c => c.Name == "国际要闻"));
            dataContext.SubmitChanges();
        }

        private static void TestUpdate()
        {
            Category categoryFinance = dataContext.Category.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();
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息