日期:2014-05-18  浏览次数:20644 次

数据库连接字符串写在配置文件中。如何在cs文件中获取它?
我的配饰文件连接代码:
C# code

<connectionStrings>
      <add name="GPD" connectionString="data source=172.16.161.*;User ID=sa;pwd=test;Initial Catalog=GPD" />
    </connectionStrings>


后台cs怎么获取它?

------解决方案--------------------
http://www.cnblogs.com/wf5360308/articles/1154962.html

1.<connectionStrings> 
<connectionStrings> 
<add name="SYXTConnectionString" connectionString="Data Source=LEIKE;Initial Catalog=SYXT;User ID=sa;Password=sa" 
providerName="System.Data.SqlClient" /> 
</connectionStrings>

------解决方案--------------------
http://www.cnblogs.com/wf5360308/articles/1154962.html

1.<connectionStrings> 
<connectionStrings> 
<add name="SYXTConnectionString" connectionString="Data Source=LEIKE;Initial Catalog=SYXT;User ID=sa;Password=sa" 
providerName="System.Data.SqlClient" /> 
</connectionStrings>

ConfigurationManager.ConnectionStrings["connectionStringsName"].ToString(); 

------解决方案--------------------
C# code
<configuration>
    <appSettings>
        <add key="conStr" value="Server=.;Database=ETer;uid=sa;pwd=sa"/>
    </appSettings>
    <connectionStrings/>

------解决方案--------------------
C# code

//添加引用
using System.Configuration;

//获取配置文件连接字符串
String connStr = ConfigurationManager.ConnectionStrings["connectionString"].ToString();

------解决方案--------------------
//添加引用
using System.Configuration;

//获取配置文件连接字符串
String connStr = ConfigurationManager.ConnectionStrings["GPD"].ToString();


------解决方案--------------------
配置文件中:
</connectionStrings>
<connectionStrings>
<add name="GPD" value="data source=172.16.161.*;User ID=sa;pwd=test;Initial Catalog=GPD" />
</connectionStrings>

CS中
//添加引用 
using System.Configuration; 

//获取配置文件连接字符串 
String connStr = ConfigurationManager.ConnectionStrings["GPD"].ToString(); 


------解决方案--------------------
string connstr=System.Configuration.ConfigurationSettings.AppSettings["GPD"];