如何从会话变量的配置文件中动态选择连接字符串?

时间:2018-01-15 07:08:49

标签: asp.net connection-string

尝试创建的是一个单一的登录屏幕,根据每个用户拥有的特定代码将用户连接到不同的数据库。

我在配置文件中创建了一些与用户代码对应的密钥,如下所示

<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 Imports 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)

返回空引用

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

经过长时间的喧嚣,我发现它创建了这个表达式构建器类

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