在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