[英]Change SqlDataSource ConnectionStrings value in code behind (vb.net)
I currently have this code in the front of my ASP.NET page 我目前在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>
Which works fine. 哪个工作正常。
However, I want to set or define the value of 'ConnectionStrings' using the codebehind - based on a session variable. 但是,我想基于会话变量使用代码隐藏来设置或定义'ConnectionStrings'的值。
My web.config contains: 我的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>
So I wish to set either ConnectionString_DEV_customer_support or ConnectionString_LIVE_customer_support in the code behind, depending on which server environment the user is on. 因此,我希望在后面的代码中设置ConnectionString_DEV_customer_support或ConnectionString_LIVE_customer_support,具体取决于用户所在的服务器环境。
I thought something like this would work (but it does not): 我以为这样的事情会起作用(但不会):
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
Using the code behind (I use VB, not C#), how do I set/define/specify which of my ConnectionStrings I want to use? 使用后面的代码(我使用VB,而不是C#),如何设置/定义/指定要使用的ConnectionString?
Just to add, if it makes a difference, I also want to set the SELECT in the code behind too. 只是要补充一点,如果有所作为,我也想在后面的代码中设置SELECT。
Eg. 例如。
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)
You could do something like this: 您可以执行以下操作:
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
I use the Page_Init to load the DataSource 我使用Page_Init加载数据源
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.