真的有一个烦人的时间与连接字符串.
我在一个解决方案中共同拥有两个项目. 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”,我试图将其存储在@R_301_382@中以便快速重复访问:
<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@R_301_382@添加到类库以在该项目中存储连接字符串?
不清楚My_ConnectionString在你的例子中来自哪里,但试试这个
System.Configuration.ConfigurationManager.ConnectionStrings( “My_ConnectionString”).的ConnectionString
喜欢这个
Dim DBConnection As New sqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("My_ConnectionString").ConnectionString)