怎样获得web.config文件中的连接串?
我的ASP.net中需要使用数据库连接串,
Dim MyConnectionString As String =?????‘web.config中有设置好的数据库连接串,怎么引用?
------解决方案-------------------- ConfigurationSettings.AppSettings[ "DBConfig "].ToString();
------解决方案--------------------using System.Configuration ;
string conn = ConfigurationSettings .AppSettings [ "conn "].ToString ();
//AppSettings [ "conn "]中的conn为WebConfig中的数据连接字符串
SqlConnecion sqlcon = new sqlConncection(conn);
------解决方案--------------------using System.Configuration;
如果是下面类型:
<appSettings>
<add key= "SQLConnectionString " value= "data source=winter;uid=sa;pwd=;database=pubs;pooling=true "> </add>
</appSettings>
那么:
static string _connectionString = ConfigurationManager.AppSettings[ "SQLConnectionString "];
如果是下面类型:
<connectionStrings>
<add name= "PubsConnectionString " connectionString= "Data Source=localhost;Initial Catalog=pubs;Integrated Security=True "
providerName= "System.Data.SqlClient " />
</connectionStrings>
那么:
static string _connectionString = ConfigurationManager.ConnectionStrings[ "PubsConnectionString "].ConnectionString;