Web.config中关于数据库连接字符串配置问题
2012-10-11 10:53
585 查看
在配置文件中,添加数据库连接字符串
有两种方法,在appSettings节点中和connectionStrings节点中
1.在appSettings节点中
<appSettings >
<add key="ConnectString" value="Data Source=.;Initial Catalog=StudentManager;Integrated Security=True"/>
<add key="connectionstringName" value="data source=服务器名或IP;initial catalog=数据库名;persist security info=False;user id=用户;password=密码;">
</appSettings>
获取该连接字符串的方法
string conStr=System.Configuration.ConfigurationManager.AppSettings["ConnectString"].ToString();
2.在connectionStrings节点中
<connectionStrings>
<add name="ConnectString" connectionString="Data Source=.;Initial Catalog=StudentManager;Integrated Security=True"/>
</connectionStrings>
读取该连接字符串的方法
需要在项目中引用"System.configuration"并在命名空间中添加using System.Configuration;
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString;
备注:在进行sa用户登录数据库的时候,需要手动敲上user id=sa ;password=sa
两者关系:
两节点均可以配置数据库连接字符
appSettings里还可以配置其他数据而connectionStrings 只能存连接字符串
相关文章推荐
- web.config中配置数据库连接字符串
- ASP.NET数据库连接字符串写在config.web配置文件中
- 在ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- ASP.NET数据库连接字符串写在config.web配置文件中(2)
- webconfig中配置各种数据库的连接字符串
- Asp.net中Web.config连接字符串及配置数据库sqlserver 浮兮vip解析
- 从Web.Config中读取数据库连接字符串的问题
- 关于sql access excel以及在web.config中数据库连接字符串的写法
- webconfig中配置各种数据库的连接字符串(转)
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- web.config中配置数据库连接字符串
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- ASP.NET项目中的web.config文件里配置数据库连接并在程序代码中获取连接字符串
- Asp.net中Web.config连接字符串及配置数据库sqlserver .
- 在Web.Config中配置数据库连接字符串
- 疑问:配置Web.Config时出现无法连接到 SQL Server 数据库的问题!!!(已解决)
- 关于如何得到web.config中的数据库连接字符串
- Asp.net中Web.config连接字符串及配置数据库sqlserver
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- 数据库的连接字符串web.config配置及读取