Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Sharing] MySqlExpress - Simplifies the Implementation of MySQL in C# #1251

adriancs2 started this conversation in Ideas
Discussion options

Hi, guys, yesterday, I had just published a class library called "MySqlExpress" and I'm happy to share it with you.

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

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

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

Assuming that we have a MySQL table like this:

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":

public class obPlayer
{
    int id = 0;
    string code = "";
    string name = "";
    DateTime date_register = DateTime.MinValue;
    string tel = "";
    string email = "";
    int status = 0;

    public int Id { get { return id; } set { id = value; }
    public string Code { get { return code; } set { code = value; }
    public string Name { get { return name; } set { name = value; }
    public DateTime DateRegister { get { return date_register; } set { date_register = value; }
    public string Tel { get { return tel; } set { tel = value; }
    public string Email { get { return email; } set { email = value; }
    public int Status { get { return status; } set { status = value; }
}

To get a row object of "player", in C#, we can do like this:

int id = 1;

// declare the object
obPlayer p = null;

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

        MySqlExpress m = new MySqlExpress(cmd);

        // parameterize the values
        Dictionary<string, object> 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:

// declare the object list
List<obPlayer> lst = null;

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

        MySqlExpress m = new MySqlExpress(cmd);

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

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

        conn.Close();
    }
}

Read more about MySqlExpress at the project site (Github).

Cheers, guys :)

You must be logged in to vote

Replies: 1 comment

Comment options

Hello,
I develop a tool SQLWrapper to do that automatically:

Nuget package Daikoz.SQLWrapper
github project

Additionally, if you have the opportunity to test my SQLWrapper tool, I would greatly appreciate any feedback you can provide.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
💡
Ideas
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.