Web.config配置连接数据库字符串的方法
2016-12-06 10:49
639 查看
在web.config中配置全局调用的数据库连接字符串,有两种方法。
1. 配置于appSettings节点,这是一个比较普遍的做法:
2.配置于connectionString节点
<connectionStrings>
<add name="myConnstring" connectionString="uid=sa;pwd=你的密码;database=数据库名;server=服务器名或IP地址" />
</connectionStrings>
调用:
string myConstring = System.Configuration.ConfigurationManager.ConnectionStrings["myConnstring"].ToString();
由于安装配置等原因,在webconfig配置文件中,可能会没有appsettings节点,直接用第二种方法搞掂就OK了。
1. 配置于appSettings节点,这是一个比较普遍的做法:
<appSettings> <add key="myConnstring" value="uid=sa;pwd=你的密码;database=数据库名;server=服务器名或IP地址"/> </appSettings> 调用: string myConstring = System.Configuration.ConfigurationManager.AppSettings["myConnstring"];
2.配置于connectionString节点
<connectionStrings>
<add name="myConnstring" connectionString="uid=sa;pwd=你的密码;database=数据库名;server=服务器名或IP地址" />
</connectionStrings>
调用:
string myConstring = System.Configuration.ConfigurationManager.ConnectionStrings["myConnstring"].ToString();
由于安装配置等原因,在webconfig配置文件中,可能会没有appsettings节点,直接用第二种方法搞掂就OK了。
相关文章推荐
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法
- asp.net2.0中获得web.config中数据库连接字符串的2种方法
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- asp.net2.0中读取web.config连接字符串或是其他配置信息的方法
- web.config 保存(Access)数据库连接字符串的两种方法
- 在Web.Config中配置数据库连接字符串
- 为web.config写入数据库连接字符串的方法
- 为web.config写入数据库连接字符串的方法
- 获得web.config中数据库连接字符串的2种方法
- ASP.NET数据库连接字符串写在config.web配置文件中(2)
- web.config配置连接数据库字符串
- webconfig 配置连接数据库字符串
- 为web.config写入数据库连接字符串的方法
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法
- 动态修改Web.config中数据库连接字符串的方法
- ASP.NET配置(web.config)无法连接到SQL Server 数据库的解决方法