-
Notifications
You must be signed in to change notification settings - Fork 345
-
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 :)
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
Replies: 1 comment
-
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.
Beta Was this translation helpful? Give feedback.