簡體   English   中英

如何從會話變量的配置文件中動態選擇連接字符串?

[英]How to select a connection string dynamically from the config file from a session variable?

試圖創建的是一個單一的登錄屏幕,該屏幕根據每個用戶具有的特定代碼將用戶連接到不同的數據庫。

我在配置文件中創建了一些與用戶代碼相對應的密鑰,如下所示

<appSettings>
<add key="ch001" value="h001"/>
<add key="ch002" value="h002"/>
</appSettings>

然后我創建了連接字符串,如下所示

<connectionStrings>
<add name="Dbconn_h001" connectionString="XXX" providerName="XXX"/>
<add name="Dbconn_h002" connectionString="XXX" providerName="XXX"/>
</connectionStrings>

然后我創建了一個類來獲取對應於連接字符串的鍵值,如下所示

Imports System.Web.Compilation

導入System.CodeDom導入System.ComponentModel

公共類ConnStringExpressionBuilder繼承ExpressionBuilder

Public Shared Function GetEvalData(ByVal expression As String, ByVal target As Type, ByVal entry As String) As Object

    Return System.Configuration.ConfigurationManager.ConnectionStrings("Dbconn_" & System.Configuration.ConfigurationManager.AppSettings(HttpContext.Current.Session("code").ToString))
End Function
Public Overrides Function GetCodeExpression(ByVal entry As BoundPropertyEntry, ByVal parsedData As Object, ByVal context As ExpressionBuilderContext) As CodeExpression
    Dim type1 As Type = entry.DeclaringType
    Dim descriptor1 As PropertyDescriptor = TypeDescriptor.GetProperties(type1)(entry.PropertyInfo.Name)
    Dim expressionArray1(2) As CodeExpression
    expressionArray1(0) = New CodePrimitiveExpression(entry.Expression.Trim())
    expressionArray1(1) = New CodeTypeOfExpression(type1)
    expressionArray1(2) = New CodePrimitiveExpression(entry.Name)
    Return New CodeCastExpression(descriptor1.PropertyType, New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType()), "GetEvalData", expressionArray1))
End Function

末級

問題是

System.Configuration.ConfigurationManager.AppSettings(HttpContext.Current.Session("code").ToString)

返回一個空引用

using(SqlConnection conn = new SqlConnection()) 
{
 var connString=ConfigurationManager.AppSetting["keyname"];
 conn.ConnectionString = connString;
// using the code here...
}

並保存在配置文件中,如<add key="ch001" value="YourConnectionString" />

經過一番忙碌之后,我發現了這個表達式生成器類

Public Class ConnStringExpressionBuilder
Inherits ExpressionBuilder

Public Shared Function GetEvalData(ByVal expression As String, ByVal target As Type, ByVal entry As String) As Object

    Return System.Configuration.ConfigurationManager.ConnectionStrings(System.Configuration.ConfigurationManager.AppSettings(HttpContext.Current.Session("code").ToString())).ToString()

End Function
Public Overrides Function GetCodeExpression(ByVal entry As BoundPropertyEntry, ByVal parsedData As Object, ByVal context As ExpressionBuilderContext) As CodeExpression
    Dim type1 As Type = entry.DeclaringType
    Dim descriptor1 As PropertyDescriptor = TypeDescriptor.GetProperties(type1)(entry.PropertyInfo.Name)
    Dim expressionArray1(2) As CodeExpression
    expressionArray1(0) = New CodePrimitiveExpression(entry.Expression.Trim())
    expressionArray1(1) = New CodeTypeOfExpression(type1)
    expressionArray1(2) = New CodePrimitiveExpression(entry.Name)
    Return New CodeCastExpression(descriptor1.PropertyType, New CodeMethodInvokeExpression(New CodeTypeReferenceExpression(MyBase.GetType()), "GetEvalData", expressionArray1))
End Function

末級

然后在我的標記中,我這樣叫課

<asp:SqlDataSource ID="Ds" runat="server" ProviderName="Mysql.Data.MysqlClient"
    ConnectionString="<%$ ConnStringExpression:Dbconn %>" SelectCommand="XXX"></asp:SqlDataSource>

然后從后面的代碼

Using conn = getConnect(System.Configuration.ConfigurationManager.AppSettings(Session("code").ToString()))
        conn.Open()

        Try
           //logic
        Catch ex As Exception

        End Try
        conn.Close()
    End Using

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM