web.config中配置数据库连接字符串
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
web.config中配置数据库连接字符串
<?xml version="1.0"?>
<!--
有关如何配置 应⽤程序的详细信息,请访问
/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<connectionStrings>
<add name="conn" connectionString="Data Source=localhost;Initial Catalog=登陆模块;Integrated Security=True"/>
</connectionStrings>
<appSettings>
<add key="conn" value="Data Source=(local);Database=登陆模块;Uid=sa;Pwd=xxxxx"/>
</appSettings>
</configuration>
1. 如上,可以将数据库字符串配置在<connectionStrings></connectionStrings>节中。
推荐将数据库连接字符串定义在该节中。
在.cs⽂件中调⽤该字符串的⽅法:
string str=System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection sqlstr = new SqlConnection(str);
//或者可以这样写
string str=System.Configuration.ConfigurationManager.ConnectionStrings["conn"].ToString();
SqlConnection sqlstr = new SqlConnection(str);
2. 也可以将数据库字符串配置在<appSettings></appSettings>节中,不推荐将数据库连接字符串定义在该节中。
在.cs⽂件中调⽤该字符串的⽅法:
string str=System.Configuration.ConfigurationManager.appSettings["conn"].ToString();
SqlConnection sqlstr = new SqlConnection(str);
web.config中连接字符串的写法也有很多种:
其中:Data source 等价于Server 后⾯都是跟要连接的服务器名
如:Data source = (local) 或者 Data source = localhost **注意** (local)==localhost ⽽没有(localhost)和local这两种写法;其中:Initial catalog等价于Database 后⾯都跟要连接的数据库名
如: Initial catalog = Login 或者 Database = Login
当连接字符串中出现Integrated Security=True或者SSPI时,连接语句中的 UserID, Pwd 是不起作⽤的,即采⽤windows⾝份验证模式。