Common C # database connection

Im working on this application that will transfer data from the database to another application. But I'm trying to work with a database.

I need to connect any database (Oracle, SQL, etc.) to the application, because my user database uses different databases.

I followed the tutorial http://www.splinter.com.au/using-generic-database-providers-with-c/ and I had problems connecting my MySQL test database to it.

Here is my sample code:

static void Main(string[] args)
{
    string connection = "SERVER=localhost;UID=root;PASSWORD=toor";
    string provider = "MySql.Data.MySqlClient";

    using (DbConnection conn = (DbConnection)Activator.CreateInstance(Type.GetType(provider), connection))
    {
        conn.Open();
        string sql = "SHOW DATABASES";
        using(DbCommand comm = conn.CreateCommand())
        {
            comm.CommandText = sql;
            using(DbDataReader reader = comm.ExecuteReader())
            {
                while(reader.Read())
                {
                    string owner = reader.GetString(0);
                    Console.WriteLine("{0}", owner);
                }
            }
        }
    }
    Console.ReadLine();
}

, , ArgumentNullException, , null. , . , MySQL. , ? , ?

, , .

+3
1

, Type.GetType(provider) null, Fullname

"MySql.Data.MySqlClient"

AssemblyQualifiedName

"MySql.Data.MySqlClient, MySql.Data"

, mscorlib.

GAC, Version, Culture PublicKeyToken, :

"MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"

.

+1

All Articles