共享一个连接字符串 [英] Sharing a connection string
问题描述
我开发一个类库(C#),我将用它为我的不同项目(后下)。我的类库DLL将使用这将引用我的新的DLL项目的连接字符串/数据上下文。我该怎么办呢?结果
可以说我有一个名为中电一个类库项目,和一个网站项目WP。我可以添加引用CLP.dll文件,但我怎么会通过一个连接字符串/数据上下文对象该DLL?作为CLP.dll将根据WP的连接字符串Access数据库。
I am developing a class library (C#) that i will use it for my different projects (later). My class library dll will use the connection string /data context of the project which will reference my new dll. How can i do it?
Lets say i have a class Library Project named "CLP", and a website project "WP". I can add reference to CLP.dll file but how i will pass a connection string/data context object to that dll? as CLP.dll will access db based on the connection string of the "WP".
不知道我的问题是明确的还是不行!
Not sure my problem is clear or not!
推荐答案
如果您开发类库,它需要被称为ConnectionString的只要你把它叫做项目已在其网站的连接字符串/连接字符串的ConnectionString的应用程序配置文件,那么它应该罚款。
If you develop your class library and it requires a connection string called "ConnectionString" as long as the project you call it from has a connection string in its web/app config file of "ConnectionString" then it should be fine.
因此,使用您的项目名称。你的中电类项目,在将设置您的数据访问code。使用字符串的ConnectionString连接:
So using your project names. Your "CLP" class project with your data access code in will setup a connection using the string "ConnectionString":
_serverConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
然后code反对该连接。
and then you code against that connection.
在您的Web项目(WP)的web.config文件中添加以下内容:
In the web.config file in your web project ("WP") you add the following:
<connectionStrings><add name="ConnectionString" connectionString="Data Source=.\SQL2008;Integrated Security=True" providerName="System.Data.SqlClient" /></connectionStrings>
显然指向你正在使用的WP项目中的数据源等。
Obviously pointing to the data source etc you are using for the "WP" project.
希望这有助于。
这篇关于共享一个连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!