基本上我有一个 web.config 文件,它在“connectionStrings”和“appSettings”部分都有几个数据库连接。请参见下面的示例。
<connectionStrings>
<!-- Connection String for SQL Server 2005 Express -->
<add
name="SiteSqlServer"
connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Database.mdf;"
providerName="System.Data.SqlClient" />
<!-- Connection String for SQL Server 2000/2005
<add
name="SiteSqlServer"
connectionString="Server=(local);Database=DotNetNuke;uid=;pwd=;"
providerName="System.Data.SqlClient" /> -->
</connectionStrings>
<appSettings>
<!-- Connection String for SQL Server 2005 Express - kept for backwards compatability - legacy modules -->
<add key="SiteSqlServer" value="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Database.mdf;"/>
<!-- Connection String for SQL Server 2000/2005 - kept for backwards compatability - legacy modules
<add key="SiteSqlServer" value="Server=(local);Database=DotNetNuke;uid=;pwd=;"/>
-->
<add key="InstallTemplate" value="DotNetNuke.install.config" />
<add key="AutoUpgrade" value="true" />
<add key="UseInstallWizard" value="true"/>
<add key="InstallMemberRole" value="true" />
<add key="ShowMissingKeys" value="false" />
<add key="EnableWebFarmSupport" value="false" />
<add key="EnableCachePersistence" value="false"/>
<add key="HostHeader" value="" />
<!-- Host Header to remove from URL so "www.mydomain.com/johndoe/Default.aspx" is treated as "www.mydomain.com/Default.aspx" -->
<add key="RemoveAngleBrackets" value="false" />
<!--optionally strip angle brackets on public login and registration screens-->
<add key="PersistentCookieTimeout" value="0" />
<!--use as persistent cookie expiration. Value is in minutes, and only active if a non-zero figure-->
<!-- set UsePortNumber to true to preserve the port number if you're using a port number other than 80 (the standard)
<add key="UsePortNumber" value="true" /> -->
</appSettings>
我需要做的是遍历每个连接字符串,将它们注释掉,然后将我的新连接字符串添加到 web.config 文件的两个部分。
这就是我到目前为止所拥有的 - 创建新的连接字符串正在工作,但我不确定如何循环注释掉任何原始连接字符串。
Dim Name As String = "SiteSqlServer"
Dim isNew As Boolean = False
Dim MyWebConfigPath As String = "C:\test\Web.Config"
Dim MyWebConfigFile As New XmlDocument()
MyWebConfigFile.Load(MyWebConfigPath)
Dim list As XmlNodeList = MyWebConfigFile.DocumentElement.SelectNodes(String.Format("connectionStrings/add[@name='{0}']", Name))
Dim node As XmlNode
isNew = list.Count = 0
If isNew = False Then
'Comment out existing connection settings
End If
node = MyWebConfigFile.CreateNode(XmlNodeType.Element, "add", Nothing)
Dim attribute As XmlAttribute = MyWebConfigFile.CreateAttribute("name")
attribute.Value = Name
node.Attributes.Append(attribute)
attribute = MyWebConfigFile.CreateAttribute("connectionString")
attribute.Value = "Data Source=CP5-1234\SQLEXPRESS;Initial Catalog=demo_db;User ID=demo_dbusr;Password=MyPass#1234"
node.Attributes.Append(attribute)
attribute = MyWebConfigFile.CreateAttribute("providerName")
attribute.Value = "System.Data.SqlClient"
node.Attributes.Append(attribute)
MyWebConfigFile.DocumentElement.SelectNodes("connectionStrings")(0).AppendChild(node)
MyWebConfigFile.Save(MyWebConfigPath)
这就是我想要的结果:
<connectionStrings>
<!-- Connection String for SQL Server 2005 Express -->
<!-- REMOVED BY ME ON 6/6/2012
<add
name="SiteSqlServer"
connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Database.mdf;"
providerName="System.Data.SqlClient" /> -->
<!-- Connection String for SQL Server 2000/2005
<add
name="SiteSqlServer"
connectionString="Server=(local);Database=DotNetNuke;uid=;pwd=;"
providerName="System.Data.SqlClient" /> -->
<add name="SiteSqlServer" connectionString="Data Source=CP5-1234\SQLEXPRESS;Initial Catalog=demo_db;User ID=demo_dbusr;Password=MyPass#1234" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<!-- Connection String for SQL Server 2005 Express - kept for backwards compatability - legacy modules -->
<!-- REMOVED BY ME ON 6/6/2012
<add key="SiteSqlServer" value="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Database.mdf;"/> -->
<!-- Connection String for SQL Server 2000/2005 - kept for backwards compatability - legacy modules
<add key="SiteSqlServer" value="Server=(local);Database=DotNetNuke;uid=;pwd=;"/> -->
<add name="SiteSqlServer" connectionString="Data Source=CP5-1234\SQLEXPRESS;Initial Catalog=demo_db;User ID=demo_dbusr;Password=MyPass#1234" providerName="System.Data.SqlClient" />
<add key="InstallTemplate" value="DotNetNuke.install.config" />
<add key="AutoUpgrade" value="true" />
<add key="UseInstallWizard" value="true"/>
<add key="InstallMemberRole" value="true" />
<add key="ShowMissingKeys" value="false" />
<add key="EnableWebFarmSupport" value="false" />
<add key="EnableCachePersistence" value="false"/>
<add key="HostHeader" value="" />
<!-- Host Header to remove from URL so "www.mydomain.com/johndoe/Default.aspx" is treated as "www.mydomain.com/Default.aspx" -->
<add key="RemoveAngleBrackets" value="false" />
<!--optionally strip angle brackets on public login and registration screens-->
<add key="PersistentCookieTimeout" value="0" />
<!--use as persistent cookie expiration. Value is in minutes, and only active if a non-zero figure-->
<!-- set UsePortNumber to true to preserve the port number if you're using a port number other than 80 (the standard)
<add key="UsePortNumber" value="true" /> -->
</appSettings>