可以使用mysqlconnection和sqlconnection的单一类型 [英] Single Type that mysqlconnection and sqlconnection can be used

查看:376
本文介绍了可以使用mysqlconnection和sqlconnection的单一类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我目前正在开发一个vb.net应用程序.其目的是成为一个辅助应用程序,因此它将自动创建以下各项:
数据库的存储过程
VB.net的类文件
vb.net的Windows窗体

它与MySql数据库完美配合,我也可以使其与MsSql一起使用,但是我的问题是....在我的代码中,我需要为mysql和mssql声明两个单独的连接,如下所示:

Ok I have a vb.net application I am developing at the moment. Its purpose is to be a helper application so it will automatically create each of the following:
StoredProcedures for Database
Class Files for VB.net
windows forms for vb.net

It works perfectly with MySql databases and I can get it to work also with MsSql but my question is this.... in my code I need to declare two separate connections for mysql and mssql as follows:

Protected DbConnectionMySql As MySqlConnection
Protected DbConnectionMsSql As SqlClient.SqlConnection


然后,我将按以下方式继续使用它们:


I then proceed to use these as follows:

If (frmMain.SQLServerType = "mysql") Then
                Me.DbConnectionMySql.Open()
                Me.dtSchema = Me.DbConnectionMySql.GetSchema("Columns")
                Me.DbConnectionMySql.Close()
            ElseIf frmMain.SQLServerType = "mssql" Then
                Me.DbConnectionMsSql.Open()
                Me.dtSchema = Me.DbConnectionMsSql.GetSchema("Columns")
                Me.DbConnectionMsSql.Close()
            End If



这很容易,因为它只有几行,但是使用更长的代码却很痛苦.
我想将每个连接映射或转换为可以使用的单个变量,因此我只需要编写一次代码.

我认为实现IDBConnection接口的泛型类型可以工作,但是有点让我感到困惑....任何想法,方向,帮助都值得赞赏!

这本来是一个帮助应用程序,可以自动执行开发大型应用程序所需的更耗时的任务.



This is easy as its only a few lines but its painful with longer code.
I would like to map or Cast each connection to a single variable that can be used so I only have to code once.

I am thinking a generic type that implements the IDBConnection interface would work but its kinda confusing me.... any idea, directions, help is appreciated!

This was meant to be a helper application to automate the more time consuming tasks of developing larger applications.

推荐答案

您应使用.NET Framework数据提供程序(ADO.NET) [
You should use the .NET Framework Data Providers (ADO.NET)[^], with allows you to connect to all kinds of databases.

Piet


IDbConnection是正确的方法.不必使用变量DbConnectionMySql和DbConnectionMsSql,您将只使用一个变量:
Dim _Connection as IDbConnection.
创建连接对象时,您仍然必须区分:
IDbConnection is the correct way. Instead of having to variables DbConnectionMySql and DbConnectionMsSql, you''ll use only one:
Dim _Connection as IDbConnection.
When the connection object is created, you still have to distinguish:
If (frmMain.SQLServerType = "mysql") Then
    _Connection = new MySqlConnection()
Else
    _Connection = new SqlClient.SqlConnection()
EndIf


顺便说一下,我将为数据库类型创建一个枚举.


By the way, I''d create an enum for the database types.


您应该使用 ^ ].网络工厂类. 此处 [
You should be using the factory pattern[^] with the ADO.Net Factory classes. Here[^] is an aexcellent article to start with.

Good luck!


这篇关于可以使用mysqlconnection和sqlconnection的单一类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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