程序头包含文件:
using System.Configuration;
using System.WebConfiguration;
示例:
using System.Configuration;
using System.WebConfiguration;
示例:
1System.Configuration.Configuration rootWebConfig =
2 System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
3System.Configuration.ConnectionStringSettings connString;
4if (0 < rootWebConfig.ConnectionStrings.ConnectionStrings.Count)
5{
6 connString =
7 rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];
8 if (null != connString)
9 Console.WriteLine("Northwind connection string = \"{0}\"",
10 connString.ConnectionString);
11 else
12 Console.WriteLine("No Northwind connection string");
13}
2 System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
3System.Configuration.ConnectionStringSettings connString;
4if (0 < rootWebConfig.ConnectionStrings.ConnectionStrings.Count)
5{
6 connString =
7 rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];
8 if (null != connString)
9 Console.WriteLine("Northwind connection string = \"{0}\"",
10 connString.ConnectionString);
11 else
12 Console.WriteLine("No Northwind connection string");
13}
此示例需要:
根 Web.config 文件中的 connectionStrings 元素,其中包含名为 NorthwindConnectionString 的连接。该元素可能如下所示:
1<connectionStrings>
2 <add
3 name="NorthwindConnectionString"
4 connectionString="Data Source=serverName;Initial
5 Catalog=Northwind;Persist Security Info=True;User
6 ID=userName;Password=password"
7 providerName="System.Data.SqlClient"
8 />
9</connectionStrings>
10
2 <add
3 name="NorthwindConnectionString"
4 connectionString="Data Source=serverName;Initial
5 Catalog=Northwind;Persist Security Info=True;User
6 ID=userName;Password=password"
7 providerName="System.Data.SqlClient"
8 />
9</connectionStrings>
10