我如何拥有多数据库提供商? [英] How can i have multi database provider ?

查看:106
本文介绍了我如何拥有多数据库提供商?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在Visual Studio 2010中使用C#(WPF)...

我想编写一个普通用户在数据库中进行日常记录的应用程序,对于这个普通用户,我需要嵌入"SQL Compact"之类的数据库,对于合并普通用户记录的特殊用户,我需要一个更快的数据库,例如"MS SQL"或"MySQL".

现在,有什么想法可以在我的应用程序中在数据库系统之间进行切换(例如:SQL Compact,MS SQL,MySQL或...)吗?


ps:对不起,因为英语不好^ _ ^

Hi,

i''m using C# (WPF) in visual studio 2010 ...

i want write a application that normal users make daily record in databse, for this normal users, i need embeded database like "SQL Compact", and for special user that merge normal users records, i need a faster database like "MS SQL" or "MySQL".

and now, any idea how to have ability to changing between database system (like : SQL Compact, MS SQL, MySQL or ...) in my application ?


ps: sorry for bad english ^_^

推荐答案

创建数据访问层.例如,使用诸如GetCustomers()和GetCustomerByID()之类的方法创建一个名为IRepository的接口.有一个存储库MsSqlRepository,用MS SQL特定代码实现IRepository.然后有另一个存储库MySqlRepository,用MySQL特定代码实现IRepository.依此类推,针对每个数据库提供者.

让您的所有代码都期望有一个IRepository.然后,每当需要创建IRepository的实际实例时,都要初始化实现该接口的类之一(MsSqlRepository或MySqlRepository).
Create a data access layer. For example, create an interface called IRepository with methods like GetCustomers() and GetCustomerByID(). Have one repository, MsSqlRepository, implement IRepository with MS SQL specific code. Then have another repository, MySqlRepository, implement IRepository with MySQL specific code. And so on, for each database provider.

Have all your code expect an IRepository. Then, whenever you need to create an actual instance of IRepository, initialize one of the classes that implements that interface (MsSqlRepository or MySqlRepository).


这篇关于我如何拥有多数据库提供商?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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