Linq 简明教程
LINQ - ASP.Net
作为一组 .NET 框架扩展,LINQ 是 ASP.NET 开发人员用于数据访问的首选机制。ASP.NET 3.5 有一个内置的工具 LINQDataSource 控件,可轻松在 ASP.NET 中使用 LINQ。ASP.NET 使用上述控件作为数据源。现实生活中的项目大多包含网站或 Windows 应用程序,因此为了更好地理解 LINQ 和 ASP.NET 的概念,让我们从创建利用 LINQ 特性的 ASP.NET 网站开始。
As a set of .NET framework extensions, LINQ is the preferred mechanism for data access by ASP.NET developers. ASP.NET 3.5 has a built-in tool LINQDataSource control that enables usage of LINQ easily in ASP.NET. ASP.NET uses the above-mentioned control as a data source. Real life projects mostly encompass websites or windows applications and so to understand better the concept of LINQ with ASP.NET, let’s start with creating a ASP.NET website that make use of the LINQ features.
为此,必须在您的系统上安装 Visual Studio 和 .NET 框架。在您打开 Visual Studio 后,转到“文件”→“新建”→“网站”。将打开一个弹出窗口,如下图中所示。
For this, it is essential to get installed Visual Studio and .NET framework on your system. Once you have opened Visual Studio, go to File → New → Website. A pop up window will open as shown in below figure.
现在,在左侧的模板下,将有两个用于创建网站的语言选项。选择 Visual C# 并选择 ASP.NET Empty Web Site 。
Now, under the templates in the left hand side, there will be two language options to create the website. Choose Visual C# and select ASP.NET Empty Web Site.
选择您想要将新网站保存在系统中的文件夹。然后按 OK 和 Solution Explorer ,出现包含所有 Web 文件的屏幕。在项目资源管理器中右键单击 Default.aspx,然后选择“在浏览器中查看”在浏览器中查看默认的 ASP.NET 网站。不久后,您的新 ASP.NET 网站将在网络浏览器中打开,如下面的屏幕截图所示。
Select the folder where you want to save new website on your system. Then press OK and soon Solution Explorer appears on your screen containing all the web files. Right click on Default.aspx in the Solution Explorer and choose View in Browser to view the default ASP.NET website in the browser. Soon your new ASP.NET website will open in the web browser, as shown in the following screenshot.
LINQDataSource Control
可以通过 LINQDataSource 控件,以 UPDATE, INSERT 和 DELETE 的方式在 ASP.NET 网站的页面中添加数据。绝对不需要指定 SQL 命令,因为 LINQDataSource 控件使用动态创建的命令执行此类操作。
It is possible to UPDATE, INSERT and DELETE data in the pages of ASP.NET website with the help of LINQDataSource control. There is absolutely no need for specification of SQL commands as LINQDataSource control employs dynamically created commands for such operations.
控件允许用户通过标记文本中的属性设置方便地在 ASP.NET 网页中使用 LINQ。LINQDataSource 与 SqlDataSource 以及 ObjectDataSource 等控件非常相似,因为它可以用来将页面上其他 ASP.NET 控件绑定到数据源。因此,我们必须有一个 database 来解释 LINQDataSource 控件调用的各种函数。
The control enables a user to make use of LINQ in an ASP.NET web page conveniently by property setting in the markup text. LINQDataSource is very similar to that of controls like SqlDataSource as well as ObjectDataSource as it can be used in binding other ASP.NET controls present on a page to a data source. So, we must have a database to explain the various functions invoked by the LINQDataSource Control.
在开始解释在 ASP.NET 网页表单中使用控件之前,必须打开 Microsoft Visual Studio 工具箱并将 LINQDataSource 控件拖放到 ASP.NET 网站的 .aspx 页面中(如下图所示)。
Before going to start explanation of the control usage in ASP.NET web page form, it is essential to open the Microsoft Visual Studio Toolbox and drag and drop LINQDataSource control to .aspx page of ASP.NET website like below figure.
下一步是通过选择员工记录的所有列来配置 LINQDataSource。
The next step is to configure LINQDataSource by selecting all the columns for the employee record.
现在将 GridView 控件添加到 .aspx 页面并像下图中所示一样对其进行配置。GridView 控件功能强大且具有灵活的数据处理能力。在配置控件后,它将出现在浏览器中。
Now add a GridView Control to the .aspx page and configure it like shown in below figure. The GridView control is powerful and offers flexibility to work with the data. Soon after configuring the control, it will appear in the browser.
现在可以在您的屏幕上看到 .aspx 页面的编码,如下所示:
The coding that can be viewed now on your screen for the .aspx page will be −
<!DOCTYPE html>
<html>
<head runat = "server">
<title></title>
</head>
<body>
<form id = "form1" runat = "server">
<div>
<asp:GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False"
DataKeyNames = "ContactID" DataSourceID = "LINQDataSource1">
<Columns>
<asp:BoundField DataField = "ContactID" HeaderText = "ContactID"
InsertVisible = "False" ReadOnly="True" SortExpression = "ContactID" />
<asp:CheckBoxField DataField = "NameStyle" HeaderText = "NameStyle"
SortExpression = "NameStyle" />
<asp:BoundField DataField = "Title" HeaderText = "Title" SortExpression = "Title" />
<asp:BoundField DataField = "FirstName" HeaderText = "FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField = "MiddleName" HeaderText = "MiddleName"
SortExpression = "MiddleName" />
<asp:BoundField DataField = "LastName" HeaderText = "LastName"
SortExpression = "LastName" />
<asp:BoundField DataField = "Suffix" HeaderText = "Suffix"
SortExpression = "Suffix" />
<asp:BoundField DataField = "EmailAddress" HeaderText = "EmailAddress"
SortExpression = "EmailAddress" />
</Columns>
</asp:GridView>
<br />
</div>
<asp:LINQDataSource ID = "LINQDataSource1" runat = "server"
ContextTypeName = "LINQWebApp1.AdventureWorksDataContext" EntityTypeName = ""
TableName = "Contacts">
</asp:LINQDataSource>
</form>
</body>
</html>
这里应注意,将 ContextTypeName 属性设置为表示数据库的类的属性至关重要。例如,这里给出的 LINQWebApp1.AdventureWorksDataContext 将使 LINQDataSource 和数据库之间建立所需的连接。
Here it should be noted that it is vital to set the property ContextTypeName to that of the class representing the database. For example, here it is given as LINQWebApp1.AdventureWorksDataContext as this action will make the needed connection between LINQDataSource and the database.
INSERT, UPDATE, and DELETE data in ASP.NET Page using LINQ
在严格执行所有上述步骤后,从 LINQDataSource Control 中选择 LINQDataSource Tasks 选择 all three boxes用于启用插入、更新和删除,如下图所示。
After completing all the above steps rigorously, choose the LINQDataSource Tasks from the LINQDataSource Control and choose all the three boxes for enable insert, enable update and enable delete from the same, as shown in the following screenshot.
很快,声明性标记将以以下方式显示在您的屏幕上。
Soon the declarative markup will get displayed on your screen as the following one.
<asp:LINQDataSource
ContextTypeName = "LINQWebApp1.AdventureWorksDataContext"
TableName = "Contacts"
EnableUpdate = "true"
EnableInsert = "true"
EnableDelete = "true"
ID = "LINQDataSource1"
runat = "server">
</asp:LINQDataSource>
由于有多行和多列,因此最好在 .aspx 表单上添加另一个控件,将其命名为“详细信息”或“主控件”(位于网格视图控件下方),以仅显示网格中所选行的详细信息。从详细信息控件中选择详细信息任务,并选择复选框,如下所示。
Now since there are multiple rows and columns, it is better to add another control on your .aspx form named as Detail View or Master control below the Grid View control to display only the details of a selected row of the grid. Choose the Detail View Tasks from the Detail View control and select the check boxes as shown below.
现在,只需保存更改并按 Ctrl+F5 即可在浏览器中查看页面,现在可以在详细信息控件上删除、更新和插入任何记录。
Now, just save the changes and press Ctrl + F5 to view the page in your browser where it is now possible to delete, update, insert any record on the detail view control.