连接字符串真的很烦人。
我在一个解决方案中同时拥有两个项目。充当表示层的 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/应用程序配置文件添加到类库中以在该项目中存储连接字符串?
不清楚在哪里My_ConnectionString
来自你的例子,但是试试这个
System.Configuration.ConfigurationManager.ConnectionStrings("My_ConnectionString").ConnectionString
像这样
Dim DBConnection As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("My_ConnectionString").ConnectionString)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)