是否可以在VB.NET中创建一个通用的数据库类?

时间:2023-01-15 16:46:57

We have 3 databases providers we use to connect to our databases: DB2, MS SQL, and Interbase. I would like to create a single generic database wrapper class that can be used to talk to all three just by passing in the correct connection string, username, password, and the provider desired.

我们有3个数据库提供程序用于连接到我们的数据库:DB2,MS SQL和Interbase。我想创建一个通用的数据库包装类,只需传入正确的连接字符串,用户名,密码和所需的提供程序,就可以用来与所有三个通信。

I don't want to have to add references and import all three providers in the database class. Is this possible?

我不想在数据库类中添加引用并导入所有三个提供程序。这可能吗?

I have done this before in Java using the Class.forName() function.

我以前在Java中使用Class.forName()函数完成了这个。

4 个解决方案

#1


There is an abstract factory built into .NET 2.0 or later, an example of its use would be:

.NET 2.0或更高版本中内置了一个抽象工厂,其使用示例如下:

Dim factory As System.Data.Common.DbProviderFactory
factory = System.Data.Common.DbProviderFactories.GetFactory("System.Data.SqlClient")

Dim conn As System.Data.Common.DbConnection = factory.CreateConnection()
conn.ConnectionString = "connectionString"
conn.Open()

There are methods on DbProviderFactory like CreateCommand, CreateDataAdapter, etc.

DbProviderFactory上有一些方法,比如CreateCommand,CreateDataAdapter等。

#2


If you do not want to have references to the individual providers in your application, you will need to handle this a little differently.

如果您不希望在应用程序中引用各个提供程序,则需要稍微改变一下。

There are two main options I see - the first (and easiest) would be to use a dependency injection framework to just plugin the appropriate provider at runtime. This is simple, clean, and works well.

我看到有两个主要选项 - 第一个(也是最简单的)是使用依赖注入框架在运行时插入适当的提供程序。这很简单,干净,效果很好。

You could do it yourself without that, though. Just make a general purpose base class that provides the interface, and, for each provider, make a separate assembly (so the references are separated) that implements this base class. You can then use Activator.CreateInstance to create an instance of the appropriate type at runtime.

不过,你可以自己做。只需创建一个提供接口的通用基类,并为每个提供程序创建一个单独的程序集(因此引用是分开的)来实现此基类。然后,您可以使用Activator.CreateInstance在运行时创建相应类型的实例。

#3


To expand on Patrick McDonald's answer, you can store the provider name and connection string in the <connectionStrings> section of your application configuration file. Then you don't need to have the providers hardcoded in the application:

要扩展Patrick McDonald的答案,您可以将提供程序名称和连接字符串存储在应用程序配置文件的 部分中。然后,您不需要在应用程序中对提供程序进行硬编码:

ConnectionStringSettings c = ConfigurationManager.ConnectionStrings["MyConnectionName"];
if (c != null)
{
   DbProviderFactory factory = DbProviderFactories.GetFactory(c.ProviderName);
   IDbConnection connection = factory.CreateConnection();
   connection.ConnectionString = c.ConnectionString;
   ...
}

Where your application configuration file contains a connectionStrings section something like:

您的应用程序配置文件包含connectionStrings部分的位置如下:

<connectionStrings>
  <add name="MyConnectionName" providerName="System.Data.SqlClient" connectionString="Data Source=serverName;Initial Catalog=DBName;Integrated Security=True"/>
</connectionStrings>

#4


can you use framework 3.5 sp1?

你可以使用框架3.5 sp1吗?

if yes, you should look at Linq to Entity

如果是的话,你应该看看Linq to Entity

#1


There is an abstract factory built into .NET 2.0 or later, an example of its use would be:

.NET 2.0或更高版本中内置了一个抽象工厂,其使用示例如下:

Dim factory As System.Data.Common.DbProviderFactory
factory = System.Data.Common.DbProviderFactories.GetFactory("System.Data.SqlClient")

Dim conn As System.Data.Common.DbConnection = factory.CreateConnection()
conn.ConnectionString = "connectionString"
conn.Open()

There are methods on DbProviderFactory like CreateCommand, CreateDataAdapter, etc.

DbProviderFactory上有一些方法,比如CreateCommand,CreateDataAdapter等。

#2


If you do not want to have references to the individual providers in your application, you will need to handle this a little differently.

如果您不希望在应用程序中引用各个提供程序,则需要稍微改变一下。

There are two main options I see - the first (and easiest) would be to use a dependency injection framework to just plugin the appropriate provider at runtime. This is simple, clean, and works well.

我看到有两个主要选项 - 第一个(也是最简单的)是使用依赖注入框架在运行时插入适当的提供程序。这很简单,干净,效果很好。

You could do it yourself without that, though. Just make a general purpose base class that provides the interface, and, for each provider, make a separate assembly (so the references are separated) that implements this base class. You can then use Activator.CreateInstance to create an instance of the appropriate type at runtime.

不过,你可以自己做。只需创建一个提供接口的通用基类,并为每个提供程序创建一个单独的程序集(因此引用是分开的)来实现此基类。然后,您可以使用Activator.CreateInstance在运行时创建相应类型的实例。

#3


To expand on Patrick McDonald's answer, you can store the provider name and connection string in the <connectionStrings> section of your application configuration file. Then you don't need to have the providers hardcoded in the application:

要扩展Patrick McDonald的答案,您可以将提供程序名称和连接字符串存储在应用程序配置文件的 部分中。然后,您不需要在应用程序中对提供程序进行硬编码:

ConnectionStringSettings c = ConfigurationManager.ConnectionStrings["MyConnectionName"];
if (c != null)
{
   DbProviderFactory factory = DbProviderFactories.GetFactory(c.ProviderName);
   IDbConnection connection = factory.CreateConnection();
   connection.ConnectionString = c.ConnectionString;
   ...
}

Where your application configuration file contains a connectionStrings section something like:

您的应用程序配置文件包含connectionStrings部分的位置如下:

<connectionStrings>
  <add name="MyConnectionName" providerName="System.Data.SqlClient" connectionString="Data Source=serverName;Initial Catalog=DBName;Integrated Security=True"/>
</connectionStrings>

#4


can you use framework 3.5 sp1?

你可以使用框架3.5 sp1吗?

if yes, you should look at Linq to Entity

如果是的话,你应该看看Linq to Entity