Linq to SQL訪問數(shù)據(jù)庫描述
在向大家詳細(xì)介紹Linq之前,首先讓大家了解下使用Linq to SQL訪問數(shù)據(jù)庫,包括介紹建立一個(gè)C# Console Application測(cè)試我們的ORM。
使用Linq to SQL訪問數(shù)據(jù)庫
我們首先新建一個(gè)工程。為了簡(jiǎn)單起見,我們就直接建立一個(gè)C# Console Application測(cè)試我們的ORM吧。將這個(gè)工程命名為L(zhǎng)inqToSqlDemo.Test。當(dāng)然,建好工程后,不要忘了添加對(duì)工程LinqToSqlDemo.Orm的引用,還要添加對(duì)“System.Data.Linq”命名空間的引用。
然后,我們打開Program.cs文件,將其中的內(nèi)容替換為如下測(cè)試代碼。
- 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("標(biāo)題:" + b.Title);
- Console.WriteLine("內(nèi)容:" + b.Content);
- Console.WriteLine("發(fā)布日期:" + b.Date);
- Console.WriteLine("所屬分類:" + b.Category1.Name);
- }
- }
- private static void TestInsert()
- {
- //生成分類實(shí)體類
- Category category1 = new Category()
- {
- Name = "國(guó)際要聞"
- };
- Category category2 = new Category()
- {
- Name = "體育新聞"
- };
- Category category3 = new Category()
- {
- Name = "財(cái)經(jīng)快報(bào)"
- };
- //生成公告實(shí)體類
- Bulletin bulletin1 = new Bulletin()
- {
- Content = "曼聯(lián)晉級(jí)冠軍杯四強(qiáng)",
- Date = DateTime.Now,
- Title = "曼聯(lián)晉級(jí)冠軍杯四強(qiáng)"
- };
- Bulletin bulletin2 = new Bulletin()
- {
- Content = "18:00直播亞冠首爾VS山東,敬請(qǐng)期待?。?!",
- Date = DateTime.Now,
- Title = "18:00直播亞冠首爾VS山東"
- };
- //將公告加入相應(yīng)分類
- category2.Bulletins.Add(bulletin1);
- category2.Bulletins.Add(bulletin2);
- //加入數(shù)據(jù)庫
- 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 == "國(guó)際要聞"));- dataContext.SubmitChanges();
- }
- private static void TestUpdate()
- {
- Category categoryFinance = dataContext.
Categories.Single(c => c.Name == "財(cái)經(jīng)快報(bào)");- categoryFinance.Name = "財(cái)經(jīng)新聞";
- dataContext.SubmitChanges();
- }
- static void Main(string[] args)
- {
- Console.WriteLine("===Linq to SQL 測(cè)試===");
- Console.WriteLine();
- Console.WriteLine("===測(cè)試Insert===");
- Console.WriteLine();
- TestInsert();
- Output();
- Console.WriteLine("===測(cè)試Delete===");
- Console.WriteLine();
- TestDelete();
- Output();
- Console.WriteLine("===測(cè)試Update===");
- Console.WriteLine();
- TestUpdate();
- Output();
- Console.ReadLine();
- }
- }
- }
我們先來看看這段測(cè)試程序做了什么事。剛開始,數(shù)據(jù)庫是空的,我們首先插入三個(gè)分類,并在“體育新聞”下插入兩條公告,這是對(duì)Insert的測(cè)試。接著,我們刪除了“國(guó)際要聞”分類,這是對(duì)Delete的測(cè)試。然后,我們將“財(cái)經(jīng)快報(bào)”改為“財(cái)經(jīng)新聞”,這是對(duì)Update測(cè)試。另外,整個(gè)過程的輸出當(dāng)然是對(duì)Select的測(cè)試。這樣,數(shù)據(jù)庫基本的操作都測(cè)試過了。從輸 出結(jié)果來看,我們的ORM組件運(yùn)行很順利,程序輸出正確。以上介紹使用Linq to SQL訪問數(shù)據(jù)庫。
【編輯推薦】