加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

VB.NET连接字符串(Web.Config,App.Config)

发布时间:2020-12-17 00:05:23 所属栏目:大数据 来源:网络整理
导读:真的有一个烦人的时间与连接字符串. 我在一个解决方案中共同拥有两个项目. Web表单应用程序充当表示层,以及支持它的类库,它将从数据库发送和接收数据. – 类库项目中的员工类 – Friend Class EmployeePublic Function GetEmployees() As DataSet Dim DBConn
真的有一个烦人的时间与连接字符串.

我在一个解决方案中共同拥有两个项目. 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在你的例子中来自哪里,但试试这个

System.Configuration.ConfigurationManager.ConnectionStrings( “My_ConnectionString”).的ConnectionString

喜欢这个

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读