web.config相關
在ASP.NET,我們經常會在web.config里插入大量的配置信息使得程序more flexible。某些配置信息是程序要調用到,如數據庫的connection string,這時,其實只要一句:ystem.Configuration.ConfigurationManager.AppSettings("×××"),我們就能獲得它的value. 但是,為了程序結構的合理性,讓程序的更易維護和擴展,應該為web.config文件設置一個專門的類,用于讀取配置信息。在程序中,當需要用到這些配置信息時,直接訪問該信息類,而不需要訪問web.config。
舉例,web.config的相關信息為如下:
<?xml version="1.0"?><configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="OralceConnectionString" connectionString="Data Source=MAPPA03C;Initial Catalog=DLOAD;User id=sa;Password=mappa" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<customErrors mode="Off"/>
<compilation debug="false">
</compilation>
<httpRuntime executionTimeout="20" maxRequestLength="8192" />
</system.web>
<location allowOverride="true" inheritInChildApplications="true">
<appSettings>
<add key="version" value="1.00" />
<add key="userid" value="TY06398" />
</appSettings>
</location>
</configuration>
我們在程序里新建一個專門針對web.config信息的類(configuration.vb). 并為配置信息設置為只讀屬性
Option Explicit On
Option Strict On
Imports System.Configuration
Namespace aspnet.config
Public Class ConfigurationInfo
Public Shared ReadOnly Property Version() As String
Get
Return System.Configuration.ConfigurationManager.AppSettings("version")
End Get
End Property
Public Shared ReadOnly Property Userid() As String
Get
Return System.Configuration.ConfigurationManager.AppSettings("userid")
End Get
End Property
Public Shared ReadOnly Property SQLServerConnectionString() As String
Get
Return System.Configuration.ConfigurationManager.ConnectionStrings("OracleConnectionString").ToString
End Get
End Property
End Class
End Namespace
在程序中使用配置信息時只需要用一句:
'System.Configuration.ConfigurationManager.ConnectionStrings("OracleConnectionString").ToString 為直接從web.config
'讀取配置信息
'Dim conn As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("OracleConnectionString").ToString)
Dim conn As New SqlConnection(ConfigurationInfo.OracleConnectionString)
-----------------------------------------------------------
佛对我说:你心里有尘。我用力的拭擦。

浙公网安备 33010602011771号