VB.NET 连接字符串(Web.Config、App.Config)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15446175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET Connection string (Web.Config, App.Config)
提问by Corgalas
Really having an annoying time with connection strings.
连接字符串真的很烦人。
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. ?
我在一个解决方案中有两个项目。一个 Web 窗体应用程序充当表示层,以及一个支持它的类库,它将从数据库发送和接收数据。?
-- The Employee Class within the Class Library Project --
-- 类库项目中的 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
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:
目前,该应用程序告诉我它无法访问“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>
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?
web.config 是 web 表单项目的一部分,而不是类库,这些项目是否无法相互“交谈”?我是否需要将 web/app 配置文件添加到类库中以在该项目中存储连接字符串?
回答by G. Stoynev
Not clear where My_ConnectionString
is coming from in your example, but try this
不清楚My_ConnectionString
你的例子来自哪里,但试试这个
System.Configuration.ConfigurationManager.ConnectionStrings("My_ConnectionString").ConnectionString
System.Configuration.ConfigurationManager.ConnectionStrings("My_ConnectionString").ConnectionString
like this
像这样
Dim DBConnection As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("My_ConnectionString").ConnectionString)
回答by Dave
If it's a .mdf database and the connection string was saved when it was created, you should be able to access it via:
如果它是一个 .mdf 数据库并且在创建时保存了连接字符串,您应该能够通过以下方式访问它:
Dim cn As SqlConnection = New SqlConnection(My.Settings.DatabaseNameConnectionString)
Hope that helps someone.
希望能帮助某人。
回答by samir
Connection in APPConfig
APPConfig 中的连接
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=192.168.1.25;Initial Catalog=Login;Persist Security Info=True;User ID=sa;Password=example.com" providerName="System.Data.SqlClient" />
</connectionStrings>
In Class.Cs
在 Class.Cs
public string ConnectionString
{
get
{
return System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
}
}
回答by Anders Elmén
I don't know if this still is an issue, but i prefere just to use the My.Settings in my code.
我不知道这是否仍然是一个问题,但我更喜欢在我的代码中使用 My.Settings。
Visual Studio generates a simple class with functions for reading settings from the app.config file.
Visual Studio 生成一个简单的类,其中包含用于从 app.config 文件读取设置的函数。
You can simply access it using My.Settings.ConnectionString.
您可以使用 My.Settings.ConnectionString 简单地访问它。
Using Context As New Data.Context.DataClasses()
Context.Connection.ConnectionString = My.Settings.ConnectionString
End Using
回答by samir
Public Function connectDB() As OleDbConnection
Dim Con As New OleDbConnection
'Con.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=" & DBNAME & ";Data Source=" & DBSERVER & ";Pwd=" & DBPWD & ""
Con.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DBNAME;Data Source=DBSERVER-TOSH;User ID=Sa;Pwd= & DBPWD"
Try
Con.Open()
Catch ex As Exception
showMessage(ex)
End Try
Return Con
End Function