真的有連接字符串惱人的時間。VB.NET連接字符串(Web.Config,App.Config)
我在一個解決方案中有兩個項目在一起。作爲表示層的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'檢索值從你的配置文件。如果這是正確的,那將不起作用 - 您需要以類似於@G的方式從配置文件中檢索它。斯托伊涅夫的答案在下面。 – Tim 2013-03-16 06:29:50
這可能很有用http://www.connectionstrings.com/ – ajt 2013-03-16 06:40:54