我在连接字符串方面遇到了很烦人的问题。
我有两个项目在一个解决方案中。一个Web表单应用程序作为表示层,另一个类库支持它将从数据库发送和接收数据。
-- 类库项目内的员工类 --
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. Stoynev 下面的回答中的方式从配置文件中检索它。 - Tim