vs2005 configure中的connection方式

在vs2005裏連接數據庫的字串是寫在<connectionStrings>節裏的,具體如下:
 
<configuration>
<connectionStrings>
    <add name="SQLCONNECTIONSTRING" connectionString="Data Source=OLDHEN;Initial Catalog=OfficeAuto;Integrated Security=True"
     providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
 
而在vs2003裏,連接數據庫的字串是寫在<appSettings>節裏的,如下所示:
 
<configuration>
<appSettings>
    <add key="SQLCONNECTIONSTRING" value="Data Source=OLDHEN;Initial Catalog=OfficeAuto;Integrated Security=True"
     providerName="System.Data.SqlClient" />
</appSettings>
</configuration>
 
注意紅色字的區別。
 
在頁面裏調用數據庫連接也因此有所區別。
在vs2005裏使用下列語句來獲取連接字符串:
 
string connstring = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ToString();
 
而在vs2003裏則使用下列語句來獲取:
 
string connstring = ConfigurationSettrings.AppSettings
["SQLCONNECTIONSTRING"];
 
ConfigurationManager與ConfigurationSettrings同屬於System.Configuration命名空間,所以要使用 using System.Configuration
  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章