C# LINQ 如何从数据库获取数据源?

发布于 2024-12-06 15:22:06 字数 298 浏览 3 评论 0 原文

我正在尝试在 C# 程序中使用 LINQ 从数据库获取信息。我找到了很多显示基本到高级查询的示例,但我找不到任何获得基本数据库连接的内容。请参阅下面的基本 LINQ 示例:

var adultNames = from person in people
                 where person.Age >= 18
                 select person.Name; 

我只是不知道如何获取特定的数据库表并从中返回信息。我希望能够连接到数据库表并从中返回一些信息。谢谢!

I am trying use LINQ in a C# program to get information from a database. I have found a lot of examples showing basic to advanced queries, but I can't find anything to get a basic database connection. See basic LINQ example below:

var adultNames = from person in people
                 where person.Age >= 18
                 select person.Name; 

I just can't figure out how to get to a specific database table and return information out of it. I want to be able to connect to a db table and return some information out of it. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

恏ㄋ傷疤忘ㄋ疼 2024-12-13 15:22:06

有一种非常简单的方法来连接到数据库,尽管我建议您随后阅读应用程序配置和项目的其他部分以了解它是如何工作的。

在菜单条中转到数据 ->添加新数据源。从这里选择数据库,然后选择实体数据模型。按照其余步骤为您的项目创建数据源。

在您的程序中,您可以使用以下方法创建数据源的对象:

WhatYouCalledTheDataSource datasourceObject = new WhatYouCalledTheDataSource();

然后,您可以通过将查询的第一行更改为来对该对象进行查询:

var adultNames = from person in datasourceObject.people 

要获取所需的数据,一旦获得查询,您可以将结果放入列表中,例如:

List<string> queryResults = new List<string>();

queryResults.AddRange(adultNames);

请记住,我基于这样的想法名称将是一个字符串。
您还可以根据表格创建列表,例如

var adultNames = from person ...
                 ...
                 select person;
List<people> queryResults = new List<people>();
queryResults.AddRange(adultNames);

Theres a very simple way to connect to a database although I suggest you read the app config afterwards and other parts of your project to see how it works.

In the menu strip go to Data -> Add New Data Source. From here pick database then entity data model. Follow the rest of the steps to create a datasource for your project.

In your program you can create an object of the datasource by using:

WhatYouCalledTheDataSource datasourceObject = new WhatYouCalledTheDataSource();

You can then make queries on this object by changing your first line of the query to:

var adultNames = from person in datasourceObject.people 

to get the data you want, once you have the query you can place the results into a list like:

List<string> queryResults = new List<string>();

queryResults.AddRange(adultNames);

bearing in mind that I basing on the thought that name would be a string.
You can also create lists based on tables like

var adultNames = from person ...
                 ...
                 select person;
List<people> queryResults = new List<people>();
queryResults.AddRange(adultNames);
无声静候 2024-12-13 15:22:06

听起来您好像一直在查看 LINQ 的示例,它允许您从数据集进行查询。您还需要一个对象关系映射器(ORM)。查看 LINQ to SQL 实体框架(较新)开始使用。该层将内省数据库并创建大量基础设施来根据数据模型为您提取数据。

It sounds like you have been looking at examples of LINQ, which allows you to query from a data set. What you need in addition is called an Object Relational Mapper (ORM). Look at LINQ to SQL or The Entity Framework (newer) to get starts with this. This layer will introspect a database and create a lot of infrastructure to pull data out for you based on the data model.

桃酥萝莉 2024-12-13 15:22:06

有多种方法可以从数据库中检索数据。 ADO.NET 是通用技术。您的具体选项是:

System.Data/SqlClient/ODP

您可以使用 ADO.NET 中的标准查询系统从任何使用 ODBC 的数据库获取数据,或使用本机数据提供程序(例如 SqlClient 或 ODP) 。 SqlClient 命名空间可用于从 SQL Server 获取数据。 Oracle Data Provider (ODP) 与 Oracle 一起使用。

您必须使用“原始”ADO.NET 自己编写查询/存储过程。

以下是 ADO.NET 示例代码的优秀列表: http://msdn.microsoft .com/en-us/library/dw70f090.aspx

以下是 Oracle ODP 的链接:
http://www.oracle.com/technetwork/topics/dotnet/index -085163.html

LINQ to SQL

LINQ to SQL 是专门针对 SQL Server 数据库的 LINQ 提供程序。它仍然受支持,但已被实体框架取代。

更多详细信息请参见: http ://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

实体框架

实体框架本身可以与 SQL Server 配合使用,并且可以使用第三方或开源数据提供程序与 Oracle 配合使用。

实体框架 4/4.1 将允许您使用实体数据模型 (EDM) 作为数据库的抽象。 EDM 将显示一组可以与之交互的对象,并且 Context 对象用于检索数据/向数据库提交数据。 CRUD 查询是根据 LINQ 语法动态生成的,因此在大多数情况下您不必自己编写 T-SQL 或 PL-SQL 查询。

这是实体框架的基本示例:
http://www.codeproject.com/KB/database/sample_entity_framework.aspx

There are a number of ways to retrieve data from a database. ADO.NET is the general technology. Your specific options are:

System.Data/SqlClient/ODP

You can use the standard query system in ADO.NET to get data from any database that uses ODBC, or use native data providers such as SqlClient or ODP. The SqlClient namespace may be used to get data from SQL Server. The Oracle Data Provider (ODP) is used with Oracle.

You have to write the queries/stored procedures yourself using "raw" ADO.NET.

Here is an excellent listing of ADO.NET example code: http://msdn.microsoft.com/en-us/library/dw70f090.aspx

Here is a link to Oracle's ODP:
http://www.oracle.com/technetwork/topics/dotnet/index-085163.html

LINQ to SQL

LINQ to SQL is a LINQ provider specifically for the SQL Server database. It is still supported, but is superseded by Entity Framework.

More detail here: http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

Entity Framework

Entity Framework works natively with SQL Server and can work with Oracle using third-party or open-source data providers.

Entity Framework 4/4.1 will allow you to use an Entity Data Model (EDM) as an abstraction of your database. The EDM will surface a set of objects with which you can interact, and a Context object is used to retrieve/commit data to the database. The CRUD queries are dynamically generated from your LINQ syntax, so you don't have to write T-SQL or PL-SQL queries yourself in most cases.

Here is a basic example of Entity Framework:
http://www.codeproject.com/KB/database/sample_entity_framework.aspx

﹉夏雨初晴づ 2024-12-13 15:22:06

如果您有一个数据库表 people,那么您可以使用 LINQ-to-SQL 从中返回信息,首先您需要引用以下两个程序集。

using System.Data.Linq;
using System.Data.Linq.Mapping;

像这样定义以下实体:

[table]
class people
{
    [column]
    public int Age { get; set; };
    [column] 
    public string Name { get; set; };
}

然后您可以针对数据库编写旅游查询

Datacontext db = new DataContext("database Connection String");
var adultNames = from person in db.GetTable<people>()
             where person.Age >= 18
             select person.Name; 

if you have a database table people then you can use LINQ-to-SQL to return information from it, first you need to reference the following two assemblies.

using System.Data.Linq;
using System.Data.Linq.Mapping;

define the following entity like this:

[table]
class people
{
    [column]
    public int Age { get; set; };
    [column] 
    public string Name { get; set; };
}

then you can write tour query against database

Datacontext db = new DataContext("database Connection String");
var adultNames = from person in db.GetTable<people>()
             where person.Age >= 18
             select person.Name; 
转角预定愛 2024-12-13 15:22:06

如果您查看了这个创建 MVC 解决方案的 NerdDinner pdf 文件,它逐步显示如何使用 Linq to SQL 创建数据集

If you have a look at this NerdDinner pdf file that creates a MVC solution, it shows step by step how to create a dataset using Linq to SQL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文