【发布时间】:2023-04-07 07:30:01
【问题描述】:
我试图创建的是一个单一的登录屏幕,它根据每个用户拥有的特定代码将用户连接到不同的数据库。
我在我的配置文件中创建了一些与用户代码相对应的键,如下所示
<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)
返回一个空引用
【问题讨论】:
-
您的
Session("code")可能为NULL 或不是ch001或ch002。调试您的代码并检查 Session 的值。