Beginning ASP.NET 3.5 in C# 2008 From Novice to Professional Second Edition(ASP.NET 3.5(C#2008)入门 )翻译-第十章(1)

At the beginning of this book, you learned that ASP.NET is just one component in Microsoft’s ambitious .NET platform. As you know, .NET also includes new languages, a new philosophy for cross-language integration, an easier way to deploy code, and a toolkit of classes that allows you to do everything from handling errors to analyzing XML documents. In this chapter,you’ll explore another one of the many features in the .NET Framework: the ADO.NET data access model.

在本书开始,您了解到, ASP.NET只是微软的雄心勃勃的.NET平台一个组成部分。正如你所知,.NET中还包括新的语言,一个新的哲学,为跨语言整合,更简单的方法来部署代码和一个类的工具包中,让您尽一切所能处理错误来分析XML文档。在这一章中, 您将探讨另一个.NET框架众多功能之一:ADO.NET数据访问模式。

Quite simply, ADO.NET is the technology that .NET applications use to interact with a database. In this chapter, you’ll learn about ADO.NET and the family of objects that provides its functionality. You’ll also learn how to put these objects to work by creating simple pages that retrieve and update database records. However, you won’t learn about the easiest way to use ADO.NET—with the help of ASP.NET data binding. Although data binding is a powerful and practical feature, every ASP.NET developer should start with a solid grasp of ADO.NET fundamentals. That’s because you’ll need to write your own ADO.NET code to optimize performance-sensitive database routines, to perform data tasks that aren’t covered by the data binding model, and to craft database components (as described in Chapter 23). Once you’ve mastered the basics of ADO.NET in this chapter, you’ll be ready to explore the timesaving shortcuts of the data binding model in Chapter 16 and Chapter 17.

很简单, ADO.NET是一项.NET应用程序用来与数据库交互的技术。在这一章中,你会了解ADO.NET和为它提供功能的对象的成员。您还可以学习如何让这些对象运作起来创造简单的检索和更新数据库记录的网页。然而,你不会了解使用ADO.NET的最简单的方法——在ASP.NET数据绑定的帮助下。虽然数据绑定是一个功能强大的和实用的功能,每一个ASP.NET的开发者应开始就牢牢的掌握ADO.NET基础。这是因为您需要撰写自己的ADO.NET代码优化性能敏感的数据库程序,执行任务,数据未涵盖的数据绑定模式,并制定数据库组件(如描述的第23章)。一旦您掌握了这一章里的ADO.NET基础在,您将准备探索第16章和17章里的节约时间快捷的数据绑定模型。

Understanding Data Management

Almost every piece of software ever written works with data. In fact, a typical web application is often just a thin user interface shell on top of sophisticated data-driven code that reads and writes information from a database. Often, website users aren’t aware (or don’t care) that the displayed information originates from a database. They just want to be able to search your product catalog, place an order, or check their payment records.

了解数据管理

几乎每写的一段软件考数据工作。事实上,一个典型的web应用往往只是一个薄薄的用户界面外壳,在它上面由精密的数据驱动的代码来从数据库读取和写入的信息。通常,网站的用户不知道(或不关心)显示的信息来自一个数据库。他们只是希望能够搜寻您的产品目录,建立一个秩序,或检查其付款记录。

The Role of the Database

The most common way to manage data is to use a database. Database technology is particularly useful for business software, which typically requires sets of related information. For example, a typical database for a sales program consists of a list of customers, a list of products, and a list of sales that draws on information from the other two tables. This type of information is best described using a relational model, which is the philosophy that underlies all modern database products, including SQL Server, Oracle, and even Microsoft Access.

数据库的角色

最常见的管理数据方式是使用一个数据库。数据库技术尤其对商业软件有用,即通常需要设置的相关信息。例如,一个典型的销售程序的数据库包括的客户名单,产品名单,和从其它两个表中抽取的销售信息。这种类型的信息是使用关系模型的最好说明,这是构成所有现代数据库产品,包括SQL Server OracleMicrosoft Access的基础理念。

As you probably know, a relational model breaks information down to its smallest and most concise units. For example, a sales record doesn’t store all the information about the products that were sold. Instead, it stores just a product ID that refers to a full record in a product table, as shown in Figure 15-1.

正如你可能知道的,一个关系模型将信息分解成最小的和最简洁的单位。例如,销售记录不储存的所有已售出产品的信息。相反,它只存储指向全部产品记录的的product表的产品ID号,如图15-1

 

 

15-1 基本的表关系

 

Although it’s technically possible to organize data into tables and store it on the hard drive in one or more files (perhaps using a standard like XML), this approach wouldn’t be very flexible. Instead, a web application needs a full relational database management system (RDBMS), such as SQL Server. The RDBMS handles the data infrastructure, ensuring optimum performance and reliability. For example, the RDBMS takes the responsibility of providing data to multiple users simultaneously, disallowing invalid data, and committing groups of actions at once using transactions.

虽然这是技术上是可行的,将数据组织成表,用一个或多个文件存储在硬盘驱动器里(或许像使用标准的XML一样),这种做法将不会很灵活。相反,一个web应用程序需要一个完整的关系数据库管理系统(关系型数据库管理系统),如SQL Server 。该数据库管理系统处理数据的基础结构,确保最佳性能和可靠性。例如,RDBMS负责同时给多个用户提供数据存储,驳回无效数据,并使用事务一次性提交一组行为。

In most ASP.NET applications, you’ll need to use a database for some tasks. Here are some basic examples of data at work in a web application:

在大多数的ASP.NET应用程序里,您为某些任务需要使用一个数据库。以下是一些在网络应用程序中使用数据的基本例子:

E-commerce sites (like Amazon) use detailed databases to store product catalogs. They

also track orders, customers, shipment records, and inventory information in a huge

arrangement of related tables.

•电子商务网站(如Amazon)使用详细的数据库存储产品目录。他们还在一系列巨大的编排好相关关系的表格中跟踪订单,客户,发货记录和库存信息。

Search engines (like Google) use databases to store indexes of page URLs, links, and

keywords.

•搜索引擎(如谷歌)使用数据库来存储网页的网址索引,链接和关键字。

Knowledge bases (like Microsoft Support) use less structured databases that store vast

quantities of information or links to various documents and resources.

•知识库(如微软支援)使用更少的结构性数据库,其中存储广阔大量的信息或链接到的各种文件和资源。

Media sites (like The New York Times) store their articles in databases.

•媒体网站(如纽约时报)在数据库中存储他们的文章。

You probably won’t have any trouble thinking about where you need to use database technology in an ASP.NET application. What web application couldn’t benefit from a guest book that records user comments or a simple e-mail address submission form that uses a back-end database to store a list of potential customers or contacts? This is where ADO.NET comes into the picture. ADO.NET is a technology designed to let an ASP.NET program (or any other .NET program, for that matter) access data.

 你可能不会有任何问题,考虑在你的ASP.NET应用程序中在哪里需要使用数据库技术。什么web应用无法从一个使用后端数据库存储潜在客户或联系人清单,记录用户的意见或一个简单的e - mail地址的提交表单的留言簿受益?这就是ADO.NET 引人注意的地方。 ADO.NET是一种技术,目的是让一个ASP.NET程序(或任何其他.NET程序,对于这个问题)获得的数据。

Tip If you’re a complete database novice, you can get up to speed on essential database concepts using the video tutorials at http://msdn.microsoft.com/vstudio/express/sql/learning. There, you’ll find over nine hours of instruction that describes how to use the free SQL Server 2005 Express Edition with Visual Studio. The tutorials move from absolute basics—covering topics such as database data types and table relationships—to more advanced subject matter such as full-text search, reporting services, and network security.

提示:如果你完全是一个的数据库新手,你可以使用视频教程http://msdn.microsoft.com/vstudio/express/sql/learning加速你对数据库基本概念的了解。在那里,你会找到超过9小时的讲授,介绍了如何使用免费的Visual Studio自带的SQL Server 2005 Express版。该教程从绝对基础,涵盖了诸如数据库的数据类型和表的关系,以及更高级的如全文检索,报告服务,以及网络安全等主题。

Database Access in the Web World

Web世界的数据库访问

Accessing a database in a web application is a completely different scenario than accessing a database in a typical client-server desktop application. Most developers hone their database skills in the desktop world and run into serious problems when they try to apply what they have learned with stand-alone applications in the world of the Web. Quite simply, web applications raise two new considerations: problems of scale and problems of state.

Web应用程序中访问一个数据库,和访问中一个典型的C/S的桌面应用程序的数据库是完全不同的情景。大多数开发者在桌面世界磨练其数据库技能,当他们试图运用他们在单机应用程序中学到的技能应用到Web世界时遇到了严重的问题时。简单地说,Web应用程序带来两个新的因素:问题的规模和问题的状态。

Problems of scale are the problems that can result from the massively multiuser nature of the Web. A web application has the potential to be used by hundreds or thousands of simultaneous users. This means it can’t be casual about using server memory or limited resources such as database connections. If you design an application that acquires a database connection and holds it for even a few extra seconds, other users may notice a definite slowdown. And if you don’t carefully consider database concurrency issues (in other words, what happens when the changes from different users overlap), you can run into significant headaches, such as failed updates and inconsistent data.

问题是大规模多用户的网络性质可能导致的规模的问题。一个web应用程序可能有数百或数千同步使用的用户。这意味着它不能随便使用服务器内存或资源有限的如数据库连接。如果你设计一个应用程序,获取数据库连接,保持甚至是多余的几秒,其他用户可能会注意到一个明显的放缓。 如果你不仔细考虑数据库并发问题(换句话说,不同的用户重叠时发生的事),您可能遇到重要的令人头痛的事,如未能更新数据和不一致的数据。

Note Problems of scale can occur when developing traditional client-server desktop applications. The difference is that in most client-server applications they are far less likely to have any negative effect because the typical load (the number of simultaneous users) is dramatically lower. Database practices that might slightly hamper the performance of a client-server application can multiply rapidly and cause significant problems in a web application.

注:当开发传统的C/S桌面应用程序时,规模的问题也会发生。不同的是,在多数C/S应用程序远远低于Web应用程序可能产生任何负面影响,因为典型负载(并发用户数)显着偏低。数据库的操作对C/S程序可能造成轻微的性能限制,但在一个web应用程序中,这个性能限制可能迅速膨胀,造成重大问题。

Problems of state are problems that can result from the disconnected nature of the Internet.

As you already know, HTTP is a stateless protocol. When a user requests a page in an

ASP.NET application, the web server processes the code, returns the rendered HTML, and

closes the connection immediately. Although users may have the illusion that they are interacting

with a continuously running application, they are really just receiving a string of static

pages.

状态的国家的问题可能是由互联网断开连接的性质导致的。 正如你所知, HTTP是一种无状态协议。当用户在一个ASP.NET应用程序请求一个网页时, Web服务器处理代码,返回提供的HTML同时立即关闭连接。尽管用户可能有错觉,他们和一个不断运行的应用程序交互,实际上他们只是收到了一连串的静态网页。

Because of the stateless nature of HTTP, web applications need to perform all their work in the space of a single request. The typical approach is to connect to a database, readin formation, display it, and then close the database connection. This approach runs into difficulties if you want the user to be able to modify the retrieved information. In this scenario, the application requires a certain amount of intelligence in order to be able to identify the original record, build a SQL statement to select it, and update it with the new values.

由于无状态性质的HTTP Web应用程序需要执行其所有工作空间的一个单一的请求。典型的做法是连接到一个数据库,读取资料,显示,然后关闭数据库连接。这一办法陷入困境,如果你希望用户可以修改检索到的信息。在这种情况下, 应用程序需要一定数额的情报,以便能够找出原始记录,建立一个SQL语句以选中它,然后更新给它新的值。

Fortunately, both ASP.NET and ADO.NET are designed with these challenges in mind. As you work your way through this chapter (and the following two chapters), you’ll learn how to deal with databases safely and efficiently.

幸运的是,ASP.NETADO.NET是为这些挑战设计的。当您以您的方式阅读这一章(及以下两章) ,您将学习如何安全和有效地处理数据库。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值