打印本文 打印本文 关闭窗口 关闭窗口
Asp.net中创建和使用Ado.net(三)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1683  更新时间:2009/4/23 10:44:09  文章录入:mintao  责任编辑:mintao

Asp.net中创建和使用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 对象的 CloseDispose 方法来实现。

集成安全性和 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 a

[1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口