ASP.NET web.config里 <connectionStrings>关键字简介

本文介绍了ASP.NET中<connectionStrings>元素的基本用法,特别是<add>标签的详细配置,包括data source、providerName等参数的含义,以及如何设置连接字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<connectionString>

          <add name=""     data source = " " provideName= "" />

  </connectionString>

常见的写法如下

<connectionStrings>
<add 
name="ApplicationServices"

     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;User Instance=true"                                      

      providerName="System.Data.SqlClient" />
</connectionStrings>

<!--<add name="dbEntities" providerName="System.Data.SqlClient" connectionString="Data Source=202.200.113.213;Initial Catalog=safefood;uid=sa;pwd=111;Pooling=False; Persist Security Info=true" /> -->

各个参数的解释如下  name="ApplicationServices"  ,name代表connectionString的名字

这个是用来写链接数据库的语句的,例如,你的add name=“ApplicationServices”,那么你在链接数据库的时候直接链ApplicationServices,就不用写链接数据库的那么长的语句了。 
完整的语句是:<add name="ApplicationServices" connectionString="写你的数据库链接语句" />   
在你用的时候定义个变量,
例如:connectionstring,则:connectionstring = ConfigurationManager.ConnectionString["ApplicationServices"].ConnectionString;
这样你链接数据库的时候直接用connectionstring 就OK。
 connectionString 数据库连接串
 providerName   使用的是什么类型的数据库,例如System.Data.SqlClient ,System.Data.OracleClient
 providerName 指定值“System.Data.OracleClient”,该值指定 ASP.NET 在使用此连接字符串进行连接时应使用 ADO.NET System.Data.OracleClient 提供程序。
 
1.<connectionStrings>
<connectionStrings>
<add name="ConnectionStringName" connectionString=" Data Source=服务器名;Initial Catalog=数据库名;User ID=用户;Password=密码"
providerName="System.Data.SqlClient" />
</connectionStrings>

或是

<connectionStrings>
<add name="ConnectionStringName" connectionString="sever=服务器名;database=数据库名;User ID=用户;Password=密码"
providerName="System.Data.SqlClient" />
</connectionStrings>

在 <connectionStrings> 里,在页面还可以这样引用<%$ ConnectionString:Name%>.
三 外文解释

Name

Default

Description

Application Name

 

The name of the application, or '.Net SqlClient Data Provider' if no application name is provided.

AttachDBFilename
-or-
extended properties
-or-
Initial File Name

 

The name of the primary file, including the full path name, of an attachable database. If this setting is specified, the Initial Catalog setting must also be specified.

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

Example:
Data Source=.; Initial Catalog=; Integrated Security=true;AttachDBFileName=c:\MyFolder\MyDb.mdf 

Update:

You can use the following syntax to attach a database that lives in your "Data" directory:

AttachDBFileName=|DataDirectory|MyDb.mdf 

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.

Current Language

 

The SQL Server Language record name.

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.

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.

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.

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.

Packet Size

8192

Size in bytes of the network packets used to communicate with an instance of 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.).

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.

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.).

Workstation ID

the local computer name

The name of the workstation connecting to SQL Server.

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.

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.

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.

Max Pool Size

100

The maximum number of connections allowed in the pool.

Min Pool Size

0

The minimum number of connections allowed in the pool.

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.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值