【发布时间】:2013-03-04 23:31:40
【问题描述】:
连接字符串真的很烦人。
我在一个解决方案中同时拥有两个项目。一个充当表示层的 Web 表单应用程序,以及一个支持它的类库,它将从数据库发送和接收数据。
-- 类库项目中的Employee类--
Friend Class Employee
Public Function GetEmployees() As DataSet
Dim DBConnection As New SqlConnection(My_ConnectionString)
Dim MyAdapter As New SqlDataAdapter("exec getEmployees", DBConnection)
Dim EmployeeInfo As DataSet
MyAdapter.Fill(EmployeeInfo, "EmployeeInfo")
Return EmployeeInfo
End Function
End Class
目前应用程序告诉我它无法访问我试图存储在配置文件中以便快速重复访问的“My_ConnectionString”:
<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<connectionStrings>
<add name="My_ConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=My_DB;Integrated Security=True;"/>
</connectionStrings>
</configuration>
web.config 是 web 表单项目的一部分,而不是类库,这些项目不能相互“对话”吗?我是否需要在类库中添加一个 web/app 配置文件来在该项目中存储一个连接字符串?
【问题讨论】:
-
从您的代码看来,您正在尝试使用
My_ConnectionString从配置文件中检索值。如果这是正确的,那将不起作用 - 您需要以类似于 @G 的方式从配置文件中检索它。斯托伊涅夫的回答如下。