[英]Change SqlDataSource ConnectionStrings value in code behind (vb.net)
我目前在ASP.NET頁面的前面有此代碼
<asp:SqlDataSource ID="SqlDataSourceRecentJobs" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString_DEV_customer_support %>"
ProviderName="<%$ ConnectionStrings:ConnectionString_DEV_customer_support.ProviderName %>"
SelectCommand="SELECT job_id FROM time_recorder_jobs WHERE (deleted = 0) ORDER BY job_id DESC LIMIT 1">
</asp:SqlDataSource>
哪個工作正常。
但是,我想基於會話變量使用代碼隱藏來設置或定義'ConnectionStrings'的值。
我的web.config包含:
<connectionStrings>
<add name="ConnectionString_DEV_customer_support" connectionString="server=REMOVED;port=REMOVED;User Id=REMOVED;password=REMOVED;Persist Security Info=True;database=dev_customer_support;Sql Server Mode=True;Allow User Variables=True" providerName="MySql.Data.MySqlClient" />
<add name="ConnectionString_LIVE_customer_support" connectionString="server=REMOVED;port=REMOVED;User Id=REMOVED;password=REMOVED;Persist Security Info=True;database=customer_support;Sql Server Mode=True;Allow User Variables=True" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
因此,我希望在后面的代碼中設置ConnectionString_DEV_customer_support或ConnectionString_LIVE_customer_support,具體取決於用戶所在的服務器環境。
我以為這樣的事情會起作用(但不會):
Dim SqlDataSourceRecentJobs As New SqlDataSource()
If Session("Environment") = "LIVE" Then
strUseThisDB = "ConnectionString_LIVE_customer_support"
Else
strUseThisDB = "ConnectionString_DEV_customer_support"
End If
SqlDataSourceRecentJobs.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings(strUseThisDB).ConnectionString
使用后面的代碼(我使用VB,而不是C#),如何設置/定義/指定要使用的ConnectionString?
只是要補充一點,如果有所作為,我也想在后面的代碼中設置SELECT。
例如。
SqlDataSourceRecentJobs.SelectCommand = "SELECT time_recorder_jobs.job_id, time_recorder_jobs.user_id, time_recorder_jobs.operation_id, time_recorder_jobs.task_id, time_recorder_jobs.customer_id, time_recorder_jobs.farm_id, time_recorder_jobs.output, time_recorder_jobs.start_time, time_recorder_jobs.end_time, time_recorder_jobs.comments FROM time_recorder_jobs INNER JOIN time_recorder_users ON time_recorder_jobs.user_id = time_recorder_users.user_id INNER JOIN time_recorder_companies ON time_recorder_users.company_id = time_recorder_companies.company_id WHERE (time_recorder_jobs.deleted = @deleted) AND (time_recorder_users.company_id = @companyid) ORDER BY time_recorder_jobs.job_id DESC LIMIT 10"
SqlDataSourceRecentJobs.SelectParameters.Clear()
SqlDataSourceRecentJobs.SelectParameters.Add("@companyid", Session("CompanyID"))
SqlDataSourceRecentJobs.SelectParameters.Add("@deleted", 0)
您可以執行以下操作:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
SqlDataSourceRecentJobs = New SqlDataSource
conn = New MySqlConnection
If ConfigurationManager.ConnectionStrings("ConnectionString_DEV_customer_support").ConnectionString Is Nothing OrElse _
ConfigurationManager.ConnectionStrings("ConnectionString_DEV_customer_support").ConnectionString.Trim() = "" Then
Throw New Exception("Connection Error")
Else
conn.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString_DEV_customer_support").ConnectionString
End If
SqlDataSourceRecentJobs.ConnectionString = conn.ConnectionString
SqlDataSourceRecentJobs.ID = "SqlDataSourceRecentJobs"
SqlDataSourceRecentJobs.ProviderName = "MySql.Data.MySqlClient"
SqlDataSourceRecentJobs.SelectCommand = "SELECT * FROM time_recoder_jobs"
SqlDataSourceRecentJobs.CancelSelectOnNullParameter = False
Me.Controls.Add(SqlDataSourceRecentJobs)
End Sub
我使用Page_Init加載數據源
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.