注释:
connectionstring:连接字符串名称
127.0.0.1:数据库地址,此处为本机名
sa:数据库登录名
pwd:数据库登录密码
Power:数据库名称
第一种写法
写法:
<configuration>
<appSettings>
<add key="connectionstring" value="server=127.0.0.1;uis=sa;pwd=123456;database=Power"/>
</appSettings>
</congiguration>
调用方式:
string rode= ConfigurationManager.AppSettings["connectionstring"];
第二种写法
写法:
<configuration>
<appSettings>
<add name="connectionstring" connectionString="server=127.0.0.1;Uid=sa;pwd=123456;Database=Power"/>
</appSettings>
</congiguration>
调用方式:
string connectionstr =ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
第三种写法
写法:
<connectionStrings>
<appSettings>
<add name="connectionstring" connectionString="Data source=localhost,8085;Initial Catalog=Power;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</appSettings>
</connectionStrings>
调用方式:
string connectionstr =ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;