-2

After doing a lot of research I am still looking for an open source standalone connector / wrapper to interact with MySql.Data.dll .

I built a custom one myself, but I am not quite happy with it because I consider it to be sub-optimal in terms of performance and security. Before rebuilding it I want to be sure that I am not re-inventing the wheel.

I am looking for a Yii-like database management, with at least:

  • Automated model class builder.
  • Basic CRUD operations with these models (Create/Read/Update/Delete).
  • Bonus: complex interactions (joins / multiple creates at the same time / etc).

I am positive that I still would need to build some custom code on the top of it to fulfill all my requirements, but it would be a solid base to start working on.

Does anybody have any suggestion?

Note: Connector/Net seems to be close to what I am looking for, but I didn't find it to be straight-forward in terms of implementation, and I would like some advice before investing a lot of time on it.

Xavier Peña
  • 6,033
  • 7
  • 49
  • 91

1 Answers1

2

There are tons of different frameworks out there from very low level impementations on top of System.Data.Common to very heavy feature rich frameworks like EF.

Most of these frameworks are not MySQL specific, since ADO.NET offers a good abstraction level that is independent from the underlying database (except some SQL dialect specific details).

As a staring point I would recommand EF since it is well documentated

http://entityframework.codeplex.com/

and Dapper (Stackoverflow itself runs on Dapper afaik)

https://code.google.com/p/dapper-dot-net/

which does not support CRUD operations out of the box, but there are frameworks on top of it.

https://github.com/tmsmith/Dapper-Extensions

Update

Just a quick introducion to ADO.NET

the basic concept of every ADO.NET driver is

  • create a connection object
  • open connection
  • create a command object
  • execute command

That means every DatabaseName ADO.NET provider has a DatabaseNameConnection, DatabaseNameCommand (and some other).

this is some very basic sample code to retrive data from a mysql database

    private DataTable GetUsers()
    {
        using (var conn = new MySqlConnection("Server=localhost;Database=mysql;Uid=root"))
        using (var cmd = new MySqlCommand("SELECT * FROM user", conn))
        {
            conn.Open();
            using (var reader = cmd.ExecuteReader())
            {
                var table = new DataTable();
                table.Load(reader);
                return table;
            }
        }
    }

this is totaly hard wired to mysql because we are using MySqlConnection and MySqlCommand but what is really mysql specific here? Only the connection string and the raw sql query. But let me rewrite it, so I can get data from every database with an ADO.net provider.

A little more code but provider independant

    public void SomeMethod()
    {
        var helper = new DbHelper("MySql.Data.MySqlClient", "Server=localhost;Database=mysql;Uid=root");
        var users = helper.ExecuteDataTable("SELECT * FROM users");
    }

    public class DbHelper
    {
        private string providerName;
        private string connectionString;
        public DbHelper(string providerName, string connectionString)
        {
            this.providerName = providerName;
            this.connectionString = connectionString;
        }

        private IDbConnection CreateConnection()
        {
            var factory = DbProviderFactories.GetFactory(providerName);
            var conn = factory.CreateConnection();
            conn.ConnectionString = connectionString;
            conn.Open();
            return conn;
        }

        private IDbCommand CreateCommand(IDbConnection conn, string commandText)
        {
            var cmd = conn.CreateCommand();
            cmd.CommandText = commandText;
            return cmd;
        }

        private DataTable ExecuteDataTable(IDataReader reader)
        {
            var table = new DataTable();
            table.Load(reader);
            return table;
        }

        public DataTable ExecuteDataTable(string commandText)
        {
            using (var conn = CreateConnection())
            using (var cmd = CreateCommand(conn, commandText))
            {
                return ExecuteDataTable(cmd.ExecuteReader());
            }
        }

    }

This works if you have MySQL Connector/NET installed because it registers itself as a DbProvider machine wide, you don't even need a reference to MySql.Data.dll in your project.

If you don't want to install Connector/NET you have to - add a reference to MySql.Data.dll - add some lines to your app.config/web.config file

  <remove invariant="MySql.Data.MySqlClient" />
  <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description="Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />

</DbProviderFactories>

Now the call to DbProviderFactories.GetFactory(providerName); will give you a MySql specific DbProviderFactory implementation.

Most DAL / ORM Frameworks use this concept. Eventually they don't have to support MySQL in the first place (except if they generate sql commands which is database specific). Entity Framework makes a difference since it requires the provider itself to support it, that's why you need MySql.Data.Entities, too.

Just a quick hint. Everything I talked about is available via nuget.

Jürgen Steinblock
  • 26,572
  • 21
  • 100
  • 169
  • Thank you so much for providing some guidance instead of just downvoting. I knew it would appear like a very basic question, but as a non-initiated I found it VERY difficult to find this information on my own (that's why I decided to post it here, of course). This was one of the walls for me, that I didn't understand before => "Most of these frameworks are not MySQL specific". Of course it seems logical now that you mention it. I also wanted a dll-independent framework, which didn't seem easy to find. I'll start digging in the direction of both EF and Dapper and see which one fits better. – Xavier Peña Jun 03 '14 at 16:08
  • 1
    @Tremor I added a small example. The best idea is to start understanding the concept of ADO.NET - If you know how to read/write data via ADO, you can switch to a higher abstraction level. – Jürgen Steinblock Jun 04 '14 at 08:25
  • That was very very helpful! I have been testing it and, of all the things you mentioned, the existence of the DataTable object just blew my mind... I guess it was very basic, I feel like the character in this xkcd: http://xkcd.com/1053/ . I think it means that I no longer need a custom class for every table. Anyway I still need some custom functions for each table/object, but still. I worry a bit about performance (lots of info in a DataTable obj!), but I'll see that later. I would also prefer non-hardcoded query strings, but I'm not sure it is avoidable. Again: thanks a ton. – Xavier Peña Jun 04 '14 at 15:14
  • As an update (and for other people that might see this post) [this is a good video](https://www.youtube.com/watch?v=lT3QrMykGUY) that ties together most of the information @SchlaWiener provided, focusing on a very summarized introduction to dapper-dot-net. On the other hand I still find it difficult to just use MySql.Data.dll in the whole process (I would even say that it is not possible in the Entity Framework, see [this related post](http://stackoverflow.com/questions/4235291/how-to-connect-to-a-mysql-data-source-in-visual-studio)). – Xavier Peña Jun 05 '14 at 13:49
  • @Tremor The link is from 2010 - I can tell you that it is possible to use mysql with entity framework. If you want to use some EF features like code generation from database you have to install the connector (I would recommand using mysql installer http://dev.mysql.com/downloads/installer ) Anyway that only applies to your dev machine. `MySql.Data` and `MySql.Data.Entities` is everything you need on the customer pc. – Jürgen Steinblock Jun 06 '14 at 06:12