在后面的代码中更改SqlDataSource ConnectionStrings值(vb.net)

时间:2014-05-14 14:34:02

标签: asp.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>

哪种方法正常。

但是,我想设置或定义&#39; ConnectionStrings&#39;使用代码隐藏 - 基于会话变量。

我的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#),如何设置/定义/指定我想使用哪个ConnectionStrings?

添加,如果它有所不同,我也想在代码中设置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)

1 个答案:

答案 0 :(得分: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加载DataSource