代码之家  ›  专栏  ›  技术社区  ›  rs.

MySQL数据连接和ADO.NET

  •  2
  • rs.  · 技术社区  · 16 年前

    我想根据条件使用mysqlconnection、command和datareader或sqlconnection、command和datareader。我想对这两种类型的连接使用相同的变量

    Currently i have
    dim _c as New mySqlConnection()
    dim _cmd as New mysqlCommand()
    dim _reader as New mysqlreader()
    
    _reader = _cmd.executereader()
    'loop through _reader
     -----------------
    

    如何根据select case将前三个更改为使用sql/mysql。这可能吗?

    2 回复  |  直到 16 年前
        1
  •  4
  •   Max Toro    16 年前

    是的,这是可能的。ADO.NET为此提供了一个API。你必须这样做:

    string providerName;
    
    if (true/*add your logic here*/)
       providerName = "System.Data.SqlClient";
    else
       providerName = "MySql.Data.MySqlClient"
    
    DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);
    
    DbConnection connection = factory.CreateConnection();
    DbCommand command = factory.CreateCommand();
    
    // ... and so on
    
        2
  •  0
  •   Joe Pitz    16 年前

    完成后,请确保关闭数据读取器。