Web Config连接字符串中的可变路径

时间:2014-11-26 16:52:04

标签: asp.net vb.net

我在asp.net网站上使用Microsoft Visual Web Developer 2010 express。我有以下代码;

在文件Web配置

<configuration>
    <connectionStrings>
        <add name="ConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ReplaceToken\abcDATA.accdb;Persist Security Info=True;Jet OLEDB:Database Password=abc"
            providerName="System.Data.OleDb" />
    </connectionStrings>
    <system.web>
        <compilation debug="false" strict="false" explicit="true" targetFramework="4.0" />
    </system.web>
</configuration>

在文件Default.aspx

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
    DataSourceID="SqlDataSource1" DataTextField="PickList" 
    DataValueField="PickList"> 
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
    SelectCommand="SELECT [PickList] FROM [T_BAS_PASexport]">
</asp:SqlDataSource>

在ConnectionString行的Web Config文件中,我想在应用程序/网站运行时将单词ReplaceToken替换为当前目录路径。请有人帮助我,我该怎么做。我需要在Default.aspx.vb文件中放入什么代码,之后我需要在Default.aspx文件中进行更改。非常感谢一个简短的解释。

1 个答案:

答案 0 :(得分:0)

试试这个:

 //In file Default.aspx.cs
 public static string CustomConnectionString
 {
     get
     {
        var connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        connectionString.Replace("ReplaceToken", "TODO");
        return connectionString;
     }
 }

 //In file Default.aspx
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="<%= CustomConnectionString %>"