VB.NET连接字符串(Web.Config,App.Config) [英] VB.NET Connection string (Web.Config, App.Config)

查看:190
本文介绍了VB.NET连接字符串(Web.Config,App.Config)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用连接字符串确实很烦人。

Really having an annoying time with connection strings.

在一个解决方案中,我有两个项目。一个Web表单应用程序充当表示层,并提供一个支持它的类库,该类库将从数据库发送和接收数据。

I have two projects together in a single solution. A Web forms application acting as the presentation layer, and a class library supporting it which will send and receive data from a database.  

-班级图书馆项目中的员工班级-

-- The Employee Class within the Class Library Project --

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:

Currently the application is telling me it cannot access "My_ConnectionString" which I have attempted to store within a config file for quick repeated access:

<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 /应用程序配置文件以在该项目中存储连接字符串?

The web.config is part of the web form project and not the class library, are these projects unable to 'talk' to each other? Do I need to add a web / app config file to the class library to store a connection string within that project?

推荐答案

不是清除示例中 My_ConnectionString 的来源,但是请尝试

Not clear where My_ConnectionString is coming from in your example, but try this

System.Configuration .ConfigurationManager.ConnectionStrings( My_ConnectionString)。ConnectionString

像这样

Dim DBConnection As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("My_ConnectionString").ConnectionString)

这篇关于VB.NET连接字符串(Web.Config,App.Config)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆