Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Tutorial [Sharing] MySqlExpress - Simplifies the Usage of MySQL in C#

adriancs

Coder
Hi, guys, I have published a class library called "MySqlExpress" and I'm excited to share it here with you.

The class library stands on top of MySqlConnector and it aims to simplify the usage of MySQL in C# / .NET environment.

The primary purpose of this library is to simplify the implementation/usage of MySQL in C#. This is to encourage the rapid application development with MySQL in C#.

Project Site: https://github.com/adriancs2/MySqlExpress

Here is one of the example of what MySqlExpress can do.

Assuming that we have a MySQL table like this:
SQL:
CREATE TABLE `player` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `code` varchar(10),
  `name` varchar(300),
  `date_register` datetime,
  `tel` varchar(100),
  `email` varchar(100),
  `status` int unsigned,
  PRIMARY KEY (`id`));
MySqlExpress Helper can help to generate the C# class object of "player":
C#:
public class obPlayer
{
    public int id { get; set; }
    public string code { get; set; }
    public string name { get; set; }
    public DateTime date_register { get; set; }
    public string tel { get; set; }
    public string email { get; set; }
    public int status { get; set; }
}
To get a row object of "player", in C#, we can do like this:
C#:
int id = 1;

// declare the object
obPlayer p = null;

using (var conn = new MySqlConnection(config.ConnString))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        cmd.Connection = conn;
        conn.Open();

        MySqlExpress m = new MySqlExpress(cmd);

        // parameterize the values
        var dicParam = new Dictionary<string, object>();
        dicParam["@vid"] = id;

        p = m.GetObject<obPlayer>($"select * from player where id=@vid;",
                dicParam);

        conn.Close();
    }
}
To get multiple rows of "player" object, we can do it like this:
C#:
// declare the object list
List<obPlayer> lst = null;

using (var conn = new MySqlConnection(config.ConnString))
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        cmd.Connection = conn;
        conn.Open();

        MySqlExpress m = new MySqlExpress(cmd);

        // parameterize the values
        var dicParam = new Dictionary<string, object>();
        dicParam["@vname"] = "%adam%";

        lst = m.GetObjectList<obPlayer>($"select * from player where name like @vname;",
                dicParam);

        conn.Close();
    }
}
There are some more "simplified" methods available.

You guys are welcome to visit the github page of the project. I have included a demo project in the source code to show case the usage of MySqlExpress that you might be interested with.

Cheers, guys :)

Here's the screenshot of the Demo of using MySqlExpress included in the source code:

screenshot02.png
 
Interesting. I have only done SQLite in C#, not MySQL, but had a quick look at a MySQL example and it looks much the same. Generally, simpler is always better of course. But the standard interface is quite simple already, and not beyond anybody with a basic grasp of C# and SQL. I wonder why it needs simplifying ? Is there anything particular about the standard interface that is deemed too difficult or complicated, to the extent that it holds back RAD with C# and MySQL ?
 
Interesting. I have only done SQLite in C#, not MySQL, but had a quick look at a MySQL example and it looks much the same. Generally, simpler is always better of course. But the standard interface is quite simple already, and not beyond anybody with a basic grasp of C# and SQL. I wonder why it needs simplifying ? Is there anything particular about the standard interface that is deemed too difficult or complicated, to the extent that it holds back RAD with C# and MySQL ?
Hi cbreemer :D

You mentioned that the standard interface is "quite simple" already.

I might have a different experience or approach of how to do MySQL in C# in the past.

Can you please share an example of code from your knowledge that presents your version of "standard interface" that is "quite simple"?

Thanks pal :D I'm really interested to know
 
Back
Top Bottom