下面是我的aspx页面中的代码。我已经声明了一个名为connect的aspx变量。我想在属性ConnectionString中的SqlDataSource控件中传递它,但是,我在页面中收到错误,指出关键字不受支持:'<%'
<%
Dim environmentText = Environment.GetEnvironmentVariable("ENVIRONMENT")
Dim connString As String = "GymsConnectionString"
If Not String.IsNullOrEmpty(environmentText) Then
environmentText = environmentText.ToLower().Trim()
connString = String.Format("GymsConnectionString_{0}", environmentText)
End If
Dim connect As String = ConfigurationManager.ConnectionStrings(connString).ToString()
%>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%
=connect %>"
SelectCommand="SELECT * FROM [Table1] WHERE ([ColumnID] = @ColumnID)">
<SelectParameters>
<asp:SessionParameter Name="Column_ID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
答案 0 :(得分:0)
直接设置它。无需使用标记来嵌入它。
SqlDataSource1.ConnectionString = connect
答案 1 :(得分:0)
我有它的工作。
在aspx页面中,我删除了ConnectionString属性,并在aspx页面中删除了下面的代码块:
Dim environmentText = Environment.GetEnvironmentVariable(“ENVIRONMENT”) Dim connString As String =“GymsConnectionString”
If Not String.IsNullOrEmpty(environmentText) Then
environmentText = environmentText.ToLower().Trim()
connString = String.Format("GymsConnectionString_{0}", environmentText)
End If
Dim connect As String = ConfigurationManager.ConnectionStrings(connString).ToString()
然后在后面的代码中,我在Page load:
中设置控制连接字符串Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim environmentText = Environment.GetEnvironmentVariable("ENVIRONMENT")
Dim connString As String = "GymsConnectionString"
If Not String.IsNullOrEmpty(environmentText) Then
environmentText = environmentText.ToLower().Trim()
connString = String.Format("GymsConnectionString_{0}", environmentText)
End If
Dim connect As String = ConfigurationManager.ConnectionStrings(connString).ToString()
SqlDataSource1.ConnectionString=connect
SqlDataSource2.ConnectionString=connect