存储连接字符串的位置 [英] Where to store Connection String

查看:44
本文介绍了存储连接字符串的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个类库,其中包含我在开发的应用程序中经常使用的所有自定义类.该库被编译并作为引用添加到我的每个应用程序中,使我能够调用该库中包含的自定义类.

I am creating a Class Library which contains all the custom classes I regularly use in the applications I develop. This library is compiled and added as a Reference in each of my applications, enabling me to call on the custom classes contained within the library.

我的库中的自定义类包括多层继承,但是所有这些最终都起源于我称为"Alpha"的基类.在该类中,我还保存了我的连接字符串,以便它被库中的所有其他类继承.许多类都大量使用ADO与SQL数据库进行通信,因此使所有类都可以使用连接字符串是必不可少的.

The custom classes in my library include multiple layers of inheritance, but all ultimately have their origins in a base class I call 'Alpha'. In that class I also hold my connection string so that it is inherited by all other classes in the library. Many of the classes make significant use of ADO to communicate with a SQL database, and so having the connection string available to all the classes is essential.

我遇到的问题是,要与我的类库一起使用的每个应用程序都有其自己的数据库,因此需要一个唯一的连接字符串.因此,连接字符串不能包含在我的类库中,因为那意味着我使用我的类库的所有应用程序都将寻找相同的SQL数据库.但是,如果我从"Alpha"类中删除连接字符串,则该类库将不会编译,因为其所有使用ADO.NET的方法都在寻找该连接字符串.

The issue I have, is that each Application I want to use my Class Library with, has its own database and so needs a connection strings unique to each. Hence the connection string cannot be contained in my class library, because that would mean all the applications I use my class library with will be looking for the same SQL database. However, if I remove the connection string from the 'Alpha' class, then the Class Library will not compile because all its methods which utilise ADO.NET are looking for that Connection String.

有点像鸡肉和鸡蛋的情况.

Its a bit like a chicken and egg scenario.

即我需要Alpha类中的连接字符串才能编译类库.即连接字符串不能在类库中,因为我需要它对于每个应用程序都是唯一的

i.e. I need the connection string in the Alpha class in order to compile the class library. i.e. the connection string cannot be in the class library because I need it to be unique to each application

为此,我能想到的唯一解决方案是在使用ADO的类库中提供所有方法,该方法用于保存连接字符串,然后可以从应用程序代码中传入该参数.但这似乎不是解决此问题的一种非常简洁的方法.

The only solution I can think of for this, is to give every method in my class library that uses ADO, a parameter to hold the connection string which can then be passed in from the applications code. But this does not seem like a very concise way of overcoming this problem.

理想情况下,我想要的是每个应用程序中的单个位置来存储连接字符串.然后,即使在编写类库时,该位置也将不可用(因为它位于各个应用程序内部),因此我需要某种方法在类库中引用该位置.

Ideally what I want is a single location in each application to store the connection string. I then need some way of referring to this location in my class library even though at the time of writing my class library that location will not be available (because it is inside the individual applications).

推荐答案

通常,连接字符串在应用程序的生存期内是恒定的,因此您可以从实际客户端应用程序的app.config中提取它,并要求使用您的库,客户端应用程序将此字符串传递给您的库.

Usually the connection string is constant for the lifetime of your application, so you could extract it from the app.config of the actual client application and make a requirement to use your library that the client application pass this string to your library.

因此,我解决了在我的库中创建一个全局帮助器类的问题,该类用于存储从客户端应用程序传递来的连接字符串,然后我的内部库方法使用此全局属性来读取连接.

So I have resolved creating a global helper class in my library where I store the connection string passed from the client application, then my internal library methods use this global property to read the connection.

public static class DatabaseHelper
{
     public static DbConnectionString {get;set;}
}

然后在客户端应用程序启动期间

then the client application during its startup calls

DatabaseHelper.DbConnectionString = ConfigurationManager.ConnectionStrings["MyDbCon"].ConnectionString;

在我的书架上

public DataTable CustomerTable()
{
    using(SqlConnection cn = new SqlConnection(DataBaseHelper.DbConnectionString))
    ......
}

这篇关于存储连接字符串的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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