HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

C# class that handle MySQL db connection

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
handlemysqlthatclassconnection

Problem

I've built a class that handles all my connections to a MySql database.

These are the function I use to create the connection(not really sure about the two dispose functions) :

public class dbConnection : IDisposable
{
    public MySqlConnection conn;
    public MySqlTransaction transaction;

    private static Logger logger = LogManager.GetCurrentClassLogger();

    public dbConnection()
    {
        string host = "1.1.1.1"; 
        string usernameMySql = "admin";
        string pswMySql = "password";
        string nomeDb = "db1";

        string connStr = "Database=" + nomeDb + ";" +
                "Data Source=" + host + ";" +
                "User Id=" + usernameMySql + ";Password=" + pswMySql + "";
        conn = new MySqlConnection(connStr);
    }

    public void openConnection()
    {
        conn.Close();
        conn.Open();
        transaction = conn.BeginTransaction();
    }

    public void closeConnection()
    {
        transaction.Commit();
        conn.Close();
    }

    protected virtual void Dispose(Boolean disposing)
    {
        if (disposing)
        {
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }


These are the functions I use to read/write data:

public void ExecuteSQL(string sSql)
    {
        MySqlCommand cmd = new MySqlCommand(sSql, conn);
        cmd.ExecuteNonQuery();
    }

    public MySqlDataReader readerSQL(string sSql)
    {
        MySqlCommand cmd = new MySqlCommand(sSql, conn);
        MySqlDataReader readerSql = cmd.ExecuteReader();

        return readerSql;
    }

    protected int ExecuteSQLReturnId(string sSql)
    {
        MySqlCommand cmd = new MySqlCommand(sSql, conn);
        cmd.ExecuteNonQuery();
        return Convert.ToInt32(cmd.LastInsertedId);
    }


In others part of my project i then use

db = new dbConnection();
db.openConnection();


to create a new connection.

This code works but I'm not really sure it's the r

Solution

What's the point of dbConnection? As it stands now it is a barely-there wrapper around MySqlConnection with a built-in connection string(!) and some odd behavior when you call openConnection(). You claim this works, are you sure -- because why wouldn't conn.Close(); throw an exception if there isn't an open connection?

Why do you always open a transaction? Why is your connection string hard-coded?

This code:

  • doesn't follow capitalization standards,



  • doesn't use a using statement to encapsulate classes that implement IDisposable (which should also be used when dealing with MySqlCommand and MySqlDataReader etc.),



  • has bad variable names like readerSql, usernameMySql,...

Context

StackExchange Code Review Q#117930, answer score: 5

Revisions (0)

No revisions yet.