Lesson 01: Introduction to ADO.NET (ado.net介绍)

来源:互联网 发布:linux怎么添修改用户id 编辑:程序博客网 时间:2024/05/29 09:47
 
Lesson 01: Introduction to ADO.NET
This lesson is an introduction to ADO.NET.  It introduces primary ADO.NET concepts and objects that you will learn about in later lessons.  Here are the objectives of this lesson:
  • 了解什么是ADO.NET
  • 理解什么是data provider
  • 理解什么是connection对象
  • 理解什么是command对象
  • 理解什么是DataReader对象
  • 理解什么是DataSet对象
  • 理解什么是DataAdapter对象
Introduction
ADO.NET is an object-oriented set of libraries that allows you to interact with data sources.  Commonly, the data source is a data base, but it could also be a text file, an Excel spread sheet, or an XML file.  For the purposes of this tutorial, we will look at ADO.NET as a way to interact with a data base.
ADO.NET是一组用于和数据源进行交互的面向对象类库。通常情况下,数据源是数据库,但它同样也能够是文本文件、Excel表格或者XML文件。因为本指南的需要,我们将ADO.NET当作是一种于数据库的交互方式。
As you are probably aware, there are many different types of data bases available.  For example, there is Microsoft SQL Server, Microsoft Access, Oracle, Borland Interbase, and IBM DB2, just to name a few.  To further refine the scope of this tutorial, all of the examples will use SQL Server. 
     你可能已经注意到有许多不同类型的数据库可供使用。比如:Microsoft SQL Server, Microsoft Access, Oracle, Borland InterbaseIBM DB2等。本指南的所有例子都将使用SQL Server
You can download the Microsoft SQL Server 2000 Desktop Engine (MSDE 2000) here:
从这里可以下载得到Microsoft SQL Server 2000 Desktop Engine (MSDE 2000)
http://www.microsoft.com/sql/msde/downloads/download.asp
MSDE contains documentation on how to perform an installation.  However, for your convenience, here are quick instructions on how to install MSDE:
     MSDE包含了指导如何安装的文档。然而,为了更加方便,下面是如何安装MSDE的快速说明:
http://www.asp.net/msde/default.aspx?tabindex=0&tabid=1
MSDE 2000 is a scaled down version of SQL Server.  Therefore, everything you learn in this tutorial and all code will work with SQL Server.  The examples will use the Northwind database.  This is a tutorial is specifically for ADO.NET.  MSDE is not part of ADO.NET, but it is one of the many data sources you can interact with by using ADO.NET If you need help with MSDE 2000, I refer you to the Microsoft Web site, where you can find pertinent information on licensing and technical assistance:
http://www.microsoft.com/sql/msde/
MSDE 2000SQL Server的缩微版。因此,本指南中所有的内容和代码都是和SQL Sever有关的。实例将会使用Northwind数据库。这是对ADO.NET的特殊化的指南。MSDE并不是ADO.NET的一部分,但却是使用与ADO.NET的数据源中的一种。如果需要MSDE 2000的帮助,可以在Microsoft站点早到合适的关于许可以及技术支持的信息:
Data Providers
数据提供程序
We know that ADO.NET allows us to interact with different types of data sources and different types of data bases.  However, there isn't a single set of classes that allow you to accomplish this universally.  Since different data sources expose different protocols, we need a way to communicate with the right data source using the right protocol.  Some older data sources use the ODBC protocol, many newer data sources use the OleDb protocol, and there are more data sources every day that allow you to communicate with them directly through .NET ADO.NET class libraries. 
     ADO.NET允许和不同类型的数据源以及数据库进行交互。然而并没有与此相关的一系列类来完成这样的工作。因为不同的数据源采用不同的协议,所以对于不同的数据源必须采用相应的协议。一些老式的数据源使用ODBC协议,许多新的数据源使用OleDb协议,并且现在还不断出现更多的数据源,这些数据源都可以通过.NETADO.NET类库来进行连接。
ADO.NET provides a relatively common way to interact with data sources, but comes in different sets of libraries for each way you can talk to a data source.  These libraries are called Data Providers and are usually named for the protocol or data source type they allow you to interact with.  Table 1 lists some well known data providers, the API prefix they use, and the type of data source they allow you to interact with.
ADO.NET提供与数据源进行交互的相关的公共方法,但是对于不同的数据源采用一组不同的类库。这些类库称为Data Providers,并且通常是以与之交互的协议和数据源的类型来命名的。表1列处了一些常见的data providers,以及它们所使用的API前缀,和允许进行交互的数据源类型。
Table 1.  ADO.NET Data Providers are class libraries that allow a common way to interact with specific data sources or protocols.  The library APIs have prefixes that indicate which provider they support.

Provider Name
API Prefix
Data Source Description
ODBC Data Provider
Odbc
Data Sources with an ODBC interface.  Normally older data bases.
OleDb Data Provider
OleDb
Data Sources that expose an OleDb interface, i.e. Access or Excel.
Oracle Data Provider
Oracle
For Oracle Data Bases.
SQL Data Provider
Sql
For interacting with Microsoft SQL Server.
Borland Data Provider
Bdp
Generic access to many data bases such as Interbase, SQL Server, IBM DB2, and Oracle.

 
1.  ADO.NET Data Providers 是允许与特殊数据库或者协议采用公共方式进行交互的一组类库。类库的API前缀说明了它支持的provider

Provider
API 前缀
Data Source 描述
ODBC Data Provider
Odbc
使用ODBC接口的数据源,通常是老式数据源
OleDb Data Provider
OleDb
提供OleDb接口的数据源,比如Acess或者Excel
Oracle Data Provider
Oracle
用于Oracle数据库
SQL Data Provider
Sql
用于Microsoft SQL Server.
Borland Data Provider
Bdp
许多数据库的公共存取方式,比如Interbase, SQL Server, IBM DB2, and Oracle.

 
An example may help you to understand the meaning of the API prefix.  One of the first ADO.NET objects you'll learn about is the connection object, which allows you to establish a connection to a data source.  If we were using the OleDb Data Provider to connect to a data source that exposes an OleDb interface, we would use a connection object named OleDbConnection.  Similarly, the connection object name would be prefixed with Odbc or Sql for an OdbcConnection object on an Odbc data source or a SqlConnection object on a SQL Server data base, respectively.  Since we are using MSDE in this tutorial (a scaled down version of SQL Server) all the API objects will have the Sql prefix.  i.e. SqlConnection.
一个实例可以帮助你了解API前缀的意义。在最开始你将学习到第一个ADO.NET对象是连接对象(connection object),它允许你建立与数据源的连接。如果我们使用OleDb数据提供器连接一个暴露OleDb接口的数据源,那么我们将使用OleDbConnection。同样,连接对象的名字也会根据它连接Odbc数据源还是SQL Server数据源而分别加上Odbc或者Sql前缀,即OdbcConnectionSqlConnection。因为我们在本指南中使用的是MSDE(一个缩微版的SQL Server),所以所有的API对象都会由Sql前缀,即SqlConnection
ADO.NET Objects
ADO.NET对象
ADO.NET includes many objects you can use to work with data.  This section introduces some of the primary objects you will use.  Over the course of this tutorial, you'll be exposed to many more ADO.NET objects from the perspective of how they are used in a particular lesson.  The objects below are the ones you must know.  Learning about them will give you an idea of the types of things you can do with data when using ADO.NET.
ADO.NET包括许多你用来和数据打交道的对象。这一节介绍了一些你将使用的主要对象。本指南结束后,你将会在专门的课程中学习到更多的ADO.NET对象。下面的对象是必须了解的。学习它们能够在使用ADO.NET的时候让你了解和数据打交道会考虑哪些事情。
The SqlConnection Object
SqlConnection 对象
To interact with a data base, you must have a connection to it.  The connection helps identify the data base server, the data base name, user name, password, and other parameters that are required for connecting to the data base.  A connection object is used by command objects so they will know which data base to execute the command on.
和数据库交互,你必须连接它。连接帮助指明数据库服务器、数据库名字、用户名、密码,和连接数据库所需要的其它参数。connection对象会被command对象使用,这样就能够知道是在哪个数据库上面执行命令。
The SqlCommand Object
SqlCommand 对象
The process of interacting with a data base means that you must specify the actions you want to occur.  This is done with a command object.  You use a command object to send SQL statements to the data base.  A command object uses a connection object to figure out which data base to communicate with.  You can use a command object alone, to execute a command directly, or assign a reference to a command object to an SqlDataAdapter, which holds a set of commands that work on a group of data as described below.
与数据库交互的过程意味着你必须指明想要发生的操作。这是依靠command对象执行的。你使用command对象来发送SQL语句给数据库。command对象使用connection对象来指出与哪个数据库进行连接。你能够单独使用command对象来直接执行命令,或者将一个command对象的引用传递给SqlDataAdapter,它保存了一组能够操作下面描述的一组数据的命令。
The SqlDataReader Object
sqlDataReader对象
Many data operations require that you only get a stream of data for reading.  The data reader object allows you to obtain the results of a SELECT statement from a command object.  For performance reasons, the data returned from a data reader is a fast forward-only stream of data.  This means that you can only pull the data from the stream in a sequential manner.  This is good for speed, but if you need to manipulate data, then a DataSet is a better object to work with.
许多数据操作要求你只是读取一串数据。data reader对象允许你获得从command对象的SELECT语句得到的结果。考虑性能的因素,从data reader返回的数据都是快速的且只是“向前”的数据流。这意味着你只能按照一定的顺序从数据流中取出数据。这对于速度来说是有好处的,但是如果你需要操作数据,更好的办法是使用DataSet
The DataSet Object
DataSet对象
DataSet objects are in-memory representations of data.  They contain multiple DataTable objects, which contain columns and rows, just like normal data base tables.  You can even define relations between tables to create parent-child relationships.  The DataSet is specifically designed to help manage data in memory and to support disconnected operations on data, when such a scenario make sense.  The DataSet is an object that is used by all of the Data Providers, which is why it does not have a Data Provider specific prefix.
DataSet对象是数据在内存中的表示形式。它包括多个DataTable对象,而DataTable包含列和行,就象一个普通的数据库中的表。你甚至能够定义表之间的关系来创建主从关系(parent-child relationships)。DataSet是在特定的场景下使用――帮助管理内存中的数据并支持对数据的断开操作的。DataSet是被所有Data Providers使用的对象,因此它并不像Data Provider一样需要特别的前缀。
The SqlDataAdapter Object
SqlDataAdapter对象
Sometimes the data you work with is primarily read-only and you rarely need to make changes to the underlying data source.  Some situations also call for caching data in memory to minimize the number of data base calls for data that does not change.  The data adapter makes it easy for you to accomplish these things by helping to manage data in a disconnected mode.  The data adapter fills a DataSet object when reading the data and writes in a single batch when persisting changes back to the data base.  A data adapter contains a reference to the connection object and opens and closes the connection automatically when reading from or writing to the data base.  Additionally, the data adapter contains command object references for SELECT, INSERT, UPDATE, and DELETE operations on the data.  You will have a data adapter defined for each table in a DataSet and it will take care of all communication with the data base for you.  All you need to do is tell the data adapter when to load from or write to the data base.
某些时候你使用的数据主要是只读的,并且你很少需要将其改变至底层的数据源。同样一些情况要求在内存中缓存数据,以此来减少并不改变的数据被数据库调用的次数。Data adapter通过断开模型来帮助你方便的完成对以上情况的处理。当在一单批次的对数据库的读写操作的持续的改变返回至数据库的时候,Data adapter 填充(fillDataSet对象。data adapter包含对连接对象以及当对数据库进行读取或者写入的时候自动的打开或者关闭连接的引用。另外,data adapter包含对数据的SELECT,INSERT,UPDATEDELETE操作的command对象引用。你将为DataSet中的每一个table都定义data adapter,它将为你照顾所有与数据库的连接。所有你将做的工作是告诉data adapter什么时候装载或者写入到数据库。
Summary
总结
ADO.NET is the .NET technology for interacting with data sources.  You have several Data Providers, which allow communication with different data sources, depending on the protocols they use or what the data base is.  Regardless, of which Data Provider used, you'll use a similar set of objects to interact with a data source.  The SqlConnection object lets you manage a connection to a data source.  SqlCommand objects allow you to talk to a data source and send commands to it.  To have fast forward-only read access to data, use the SqlDataReader.  If you want to work with disconnected data, use a DataSet and implement reading and writing to/from the data source with a SqlDataAdapter.
ADO.NET是与数据源交互的.NET技术。有许多的Data Providers,它将允许与不同的数据源交流――取决于它们所使用的协议或者数据库。然而无论使用什么样的Data Provider,你将使用相似的对象与数据源进行交互。SqlConnection对象管理与数据源的连接。SqlCommand对象允许你与数据源交流并发送命令给它。为了对进行快速的只“向前”地读取数据,使用SqlDataReader。如果想使用断开数据,使用DataSet并实现能进行读取或者写入数据源的SqlDataAdapter
This is just the beginning - the first of several lessons in the ADO.NET Tutorial.  The next one in this series is Lesson 02:  The SqlConnection Object.
这只是开始――在ADO.NET指南中的第一部分的课程。这一系列的下一讲是Lesson 02:  SqlConnection 对象
For further questions, you are welcome to participate in the C# Station Discussion Forums.
Your feedback is very important and I appreciate any constructive contributions you have.  Please feel free to contact me for feedback or comments you may have about this lesson.