Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to access the database with Linq to SQL

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

本篇内容介绍了"怎么用Linq to SQL访问数据库"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

在向大家详细介绍Linq之前,首先让大家了解下使用Linq to SQL访问数据库,包括介绍建立一个C# Console Application测试我们的ORM。

使用Linq to SQL访问数据库

我们首先新建一个工程。为了简单起见,我们就直接建立一个C# Console Application测试我们的ORM吧。将这个工程命名为LinqToSqlDemo.Test。当然,建好工程后,不要忘了添加对工程LinqToSqlDemo.Orm的引用,还要添加对"System.Data.Linq"命名空间的引用。

然后,我们打开Program.cs文件,将其中的内容替换为如下测试代码。

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山东,敬请期待!!!",

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();

}

}

}

我们先来看看这段测试程序做了什么事。刚开始,数据库是空的,我们首先插入三个分类,并在"体育新闻"下插入两条公告,这是对Insert的测试。接着,我们删除了"国际要闻"分类,这是对Delete的测试。然后,我们将"财经快报"改为"财经新闻",这是对Update测试。另外,整个过程的输出当然是对Select的测试。这样,数据库基本的操作都测试过了。从输 出结果来看,我们的ORM组件运行很顺利,程序输出正确。

"怎么用Linq to SQL访问数据库"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report