Asp.net中创建和使用Ado.net

发布时间:2006年11月08日      浏览次数:4668 次
在商业应用程序中最重要的组件是数据,无论是在线的商务应用程序、公司的企业软件还是小型公司的会计应用程序无不如此:通过一个通用的线程与数据打交道,即都必须实现快速、有效、可靠的方式存储、检索和处理数据。

然而,一直一来令人棘手的问题是这些数据文件常以不同的格式存储,这就需要开发者学会用多种不同的方式来处理完全一样的事情。Microsoft等诸多数据提供者力求实现数据访问格式的标准化。

从Odbc的出现到Dao、Rdo、Oledb、Ado的实现,可以说标准化的进程逐步实现。特别是Ado的出现很好的实现了通用数据访问的模式,致使很多人认为,每隔两年学习一种新的数据访问对象模型的年代已经过去了。但是通过Internet,我们发现这种想法是错误的。

一、ADO.NET 的设计目标

随着应用程序开发的发展演变,新的应用程序已基于 Web 应用程序模型越来越松散地耦合。如今,越来越多的应用程序使用 XML 来编码要通过网络连接传递的数据。Web 应用程序将 HTTP 用作在层间进行通信的结构,因此它们必须显式处理请求之间的状态维护。这一新模型大大不同于连接、紧耦合的编程风格,此风格曾是客户端/服务器时代的标志。在此编程风格中,连接会在程序的整个生存期中保持打开,而不需要对状态进行特殊处理。

设计 ADO.NET 的目的是为了满足这一新编程模型的以下要求:具有断开式数据结构;能够与 XML 紧密集成;具有能够组合来自多个、不同数据源的数据的通用数据表示形式;在创建 ADO.NET 时,Microsoft 具有以下设计目标。

利用当前的 ADO 知识

ADO.NET 的设计满足了当今应用程序开发模型的多种要求。同时,该编程模型尽可能地与 ADO 保持一致,这使当今的 ADO 开发人员不必从头开始学习全新的数据访问技术。ADO.NET 是 .NET Framework 的固有部分,因此对于 ADO 程序员决不是完全陌生的。

ADO.NET 与 ADO 共存。虽然大多数基于 .NET 的新应用程序将使用 ADO.NET 来编写,但 .NET 程序员仍然可以通过 .NET COM 互操作性服务来使用 ADO。

支持 N 层编程模式
ADO.NET 为断开式 n 层编程环境提供了一流的支持,许多新的应用程序都是为该环境编写的。使用断开式数据集这一概念已成为编程模型中的焦点。n 层编程的 ADO.NET 解决方案就是 DataSet。

集成 XML 支持
XML 和数据访问是紧密联系在一起的,即 XML 的全部内容都是有关数据编码的,而数据访问越来越多的内容都与 XML 有关。.NET Framework 不仅支持 Web 标准,它还是完全基于 Web 标准生成的。

XML 支持内置在 ADO.NET 中非常基本的级别上。.NET Framework 和 ADO.NET 中的 XML 类是同一结构的一部分,它们在许多不同的级别集成。您不必在数据访问服务集和它们的 XML 相应服务之间进行选择;它们的设计本来就具有从其中一个跨越到另一个的功能。
二、ADO.NET 的组件

设计 ADO.NET 组件的目的是为了从数据操作中分解出数据访问。ADO.NET 的两个核心组件会完成此任务:DataSet 和 .NET Framework 数据提供程序,后者是一组包括 Connection、Command、DataReader 和 DataAdapter 对象在内的组件。

ADO.NET DataSet 是 ADO.NET 的断开式结构的核心组件。DataSet 的设计目的很明确:为了实现独立于任何数据源的数据访问。因此,它可以用于多种不同的数据源,用于 XML 数据,或用于管理应用程序本地的数据。DataSet 包含一个或多个 DataTable 对象的集合,这些对象由数据行和数据列以及主键、外键、约束和有关 DataTable 对象中数据的关系信息组成。

ADO.NET 结构的另一个核心元素是 .NET Framework 数据提供程序,其组件的设计目的相当明确:为了实现数据操作和对数据的快速、只进、只读访问。Connection 对象提供与数据源的连接。Command 对象使您能够访问用于返回数据、修改数据、运行存储过程以及发送或检索参数信息的数据库命令。DataReader 从数据源中提供高性能的数据流。最后,DataAdapter 提供连接 DataSet 对象和数据源的桥梁。DataAdapter 使用 Command 对象在数据源中执行 SQL 命令,以便将数据加载到 DataSet 中,并使对 DataSet 中数据的更改与数据源保持一致。

可以为任何数据源编写 .NET Framework 数据提供程序。.NET Framework 提供了四个 .NET Framework 数据提供程序:SQL Server .NET Framework 数据提供程序、OLE DB .NET Framework 数据提供程序、ODBC .NET Framework 数据提供程序和 Oracle .NET Framework 数据提供程序。

下图阐释了 ADO.NET 结构的组件。

三、使用 ADO.NET 连接到数据源
在 ADO.NET 中,可以使用 Connection 对象来连接到指定的数据源。若要连接到 Microsoft SQL Server 7.0 版或更高版本,请使用 SQL Server .NET Framework 数据提供程序的 SqlConnection 对象。若要使用用于 SQL Server 的 OLE DB 提供程序 (SQLOLEDB) 连接到 OLE DB 数据源或者连接到 Microsoft SQL Server 6.x 版或较早版本,请使用 OLE DB .NET Framework 数据提供程序的 OleDbConnection 对象。若要连接到 ODBC 数据源,请使用 ODBC .NET Framework 数据提供程序的 OdbcConnection 对象。若要连接到 Oracle 数据源,请使用 Oracle .NET Framework 数据提供程序的 OracleConnection 对象。

1、 使用 ADO.NET 连接到 SQL Server
SQL Server .NET Framework 数据提供程序使用 SqlConnection 对象提供与 Microsoft SQL Server 7.0 版或更高版本的连接。

SQL Server .NET Framework 数据提供程序支持类似于 OLE DB (ADO) 连接字符串格式的连接字符串格式。有关有效的字符串格式名称和值,请参见附表1

以下代码示例演示如何创建和打开与 SQL Server(版本 7.0 或更高版本)数据库的连接。

[Visual Basic]
Dim myConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")
myConn.Open()
[C#]
SqlConnection nwindConn = new SqlConnection("Data Source=localhost; Integrated Security=SSPI;" +
"Initial Catalog=northwind");
nwindConn.Open();
建议使用完 Connection 后始终将其关闭。这可以使用 Connection 对象的 Close 或 Dispose 方法来实现。

集成安全性和 ASP.NET
SQL Server 集成安全性(也称为受信任的连接)是连接到 SQL Server 的最安全的方法,因为它不在连接字符串中公开用户标识和密码。建议使用该方法对连接进行身份验证。集成安全性使用正在执行的进程的当前安全标识或标记。对于桌面应用程序,安全标识或标记通常是当前登录的用户的标识。

ASP.NET 应用程序的安全标识可设置为几个不同的选项之一。若要更好地了解使用集成安全性连接到 SQL Server 时 ASP.NET 应用程序所使用的安全标识,请参见我写得Asp.net中进行安全的 ADO.NET 编码系列或者参考msdn

Name
Default
Description
名称
默认值
说明

Application Name

The name of the application, or '.Net SqlClient Data Provider' if no application name is provided.
应用程序名称

应用程序的名称,或者“.Net SqlClient Data Provider”(如果不提供应用程序名称)。

AttachDBFilename

-or-

extended properties

-or-

Initial File Name

The name of the primary file, including the full path name, of an attachable database.

The database name must be specified with the keyword 'database'.
AttachDBFilename

- 或 -

扩展属性

- 或 -

初始文件名

可连接数据库的主文件的名称,包括完整的路径名。

必须使用关键字“database”来指定数据库的名称。

Connect Timeout

-or-

Connection Timeout
15
The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.
连接超时设定

- 或 -

连接超时
15
在终止尝试并产生错误之前,等待与服务器的连接的时间长度(以秒为单位)。

Current Language

The SQL Server Language record name.
当前语言

SQL Server 语言记录名称。

Data Source

-or-

Server

-or-

Address

-or-

Addr

-or-

Network Address

The name or network address of the instance of SQL Server to which to connect.
数据源

- 或 -

服务器

- 或 -

地址

- 或 -

Addr

- 或 -

网络地址

要连接的 SQL Server 实例的名称或网络地址。

Encrypt
'false'
When true, SQL Server uses SSL encryption for all data sent between the client and server if the server has a certificate installed. Recognized values are true, false, yes, and no.
加密
'false'
当该值为 true 时,如果服务器端安装了证书,则 SQL Server 将对所有在客户端和服务器之间传送的数据使用 SSL 加密。可识别的值为 true、false、yes 和 no。

Initial Catalog

-or-

Database

The name of the database.
初始目录

- 或 -

数据库

数据库的名称。

Integrated Security

-or-

Trusted_Connection
'false'
When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.

Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true.
集成安全性

- 或 -

Trusted_Connection
'false'
当为 false 时,将在连接中指定用户 ID 和密码。当为 true 时,将使用当前的 Windows 帐户凭据进行身份验证。

可识别的值为 true、false、yes、no 以及与 true 等效的 sspi(强烈推荐)。

Network Library

-or-

Net
'dbmssocn'
The network library used to establish a connection to an instance of SQL Server. Supported values include dbnmpntw (Named Pipes), dbmsrpcn (Multiprotocol), dbmsadsn (Apple Talk), dbmsgnet (VIA), dbmslpcn (Shared Memory) and dbmsspxn (IPX/SPX), and dbmssocn (TCP/IP).

The corresponding network DLL must be installed on the system to which you connect. If you do not specify a network and you use a local server (for example, "." or "(local)"), shared memory is used.
网络库

- 或 -

网络
'dbmssocn'
用于建立与 SQL Server 实例的连接的网络库。支持的值包括 dbnmpntw(命名管道)、dbmsrpcn(多协议)、dbmsadsn (Apple Talk)、dbmsgnet (VIA)、dbmslpcn(共享内存)及 dbmsspxn (IPX/SPX) 和 dbmssocn (TCP/IP)。

相应的网络 DLL 必须安装在要连接的系统上。如果不指定网络而使用一个本地服务器(比如“.”或“(local)”),则使用共享内存。

Packet Size
8192
Size in bytes of the network packets used to communicate with an instance of SQL Server.
数据包大小
8192
用来与 SQL Server 的实例进行通讯的网络数据包的大小,以字节为单位。

Password

-or-

Pwd

The password for the SQL Server account logging on (Not recommended. To maintain a high level of security, it is strongly recommended that you use the Integrated Security or Trusted_Connection keyword instead.).
密码

- 或 -

Pwd

SQL Server 帐户登录的密码(建议不要使用。为了维护最高级别的安全性,强烈建议改用 Integrated Security 或 Trusted_Connection 关键字)。

Persist Security Info
'false'
When set to false or no (strongly recommended), security-sensitive information, such as the password, is not returned as part of the connection if the connection is open or has ever been in an open state. Resetting the connection string resets all connection string values including the password. Recognized values are true, false, yes, and no.
持续安全信息
'false'
当该值设置为 false 或 no(强烈推荐)时,如果连接是打开的或者一直处于打开状态,那么安全敏感信息(如密码)将不会作为连接的一部分返回。重置连接字符串将重置包括密码在内的所有连接字符串值。可识别的值为 true、false、yes 和 no。

User ID

The SQL Server login account (Not recommended. To maintain a high level of security, it is strongly recommended that you use the Integrated Security or Trusted_Connection keyword instead.).
用户 ID

SQL Server 登录帐户(建议不要使用。为了维护最高级别的安全性,强烈建议改用 Integrated Security 或 Trusted_Connection 关键字)。

Workstation ID
the local computer name
The name of the workstation connecting to SQL Server.
工作站 ID
本地计算机名称
连接到 SQL Server 的工作站的名称。




Name
Default
Description
名称
默认值
说明

Connection Lifetime
0
When a connection is returned to the pool, its creation time is compared with the current time, and the connection is destroyed if that time span (in seconds) exceeds the value specified by Connection Lifetime. This is useful in clustered configurations to force load balancing between a running server and a server just brought online.

A value of zero (0) causes pooled connections to have the maximum connection timeout.
连接生存期
0
当连接被返回到池时,将其创建时间与当前时间作比较,如果时间长度(以秒为单位)超出了由 Connection Lifetime 指定的值,该连接就会被销毁。这在聚集配置中很有用(用于强制执行运行中的服务器和刚置于联机状态的服务器之间的负载平衡)。

零 (0) 值将使池连接具有最大的连接超时。

Connection Reset
'true'
Determines whether the database connection is reset when being drawn from the pool. For Microsoft SQL Server version 7.0, setting to false avoids making an additional server round trip when obtaining a connection, but you must be aware that the connection state, such as database context, is not being reset.
连接重置
'true'
确定从池中提取数据库连接时是否重置数据库连接。对于 Microsoft SQL Server 7.0 版,设置为 false 可避免获取连接时再有一次额外的服务器往返行程,但须注意此时并未重置连接状态(如数据库上下文)。

Enlist
'true'
When true, the pooler automatically enlists the connection in the creation thread's current transaction context. Recognized values are true, false, yes, and no.
登记
'true'
当该值为 true 时,池程序在创建线程的当前事务上下文中自动登记连接。可识别的值为 true、false、yes 和 no。

Max Pool Size
100
The maximum number of connections allowed in the pool.
最大池大小
100
池中允许的最大连接数。

Min Pool Size
0
The minimum number of connections allowed in the pool.
最小池大小
0
池中允许的最小连接数。

Pooling
'true'
When true, the SQLConnection object is drawn from the appropriate pool, or if necessary, is created and added to the appropriate pool. Recognized values are true, false, yes, and no.

'true'
当该值为 true 时,系统将从相应池中提取 SQLConnection 对象,或在必要时创建该对象并将其添加到相应池中。可识别的值为 true、false、yes 和 no。
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!