Results 1 to 1 of 1

Thread: MySQL Connection

  1. #1

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    MySQL Connection

    Here is my MySQL connection class. It encapsulates a MySQL connection and the associated ExecuteReader, ExecuteScalar and ExecuteNonQuery methods. The class's constructor accepts a connection string directly, or you can establish a connection using one of the GetConnection Shared methods which accept database credentials or a connections string.

    I wrote this class a few years ago with the intent of have a database/namespace independent connection insofar as the user is concerned. I planned to develop an Interface and have various sub classes for the different database types. The GetConnection method would then accept an enumeration instructing it to return the specific Connection class. I only got as far as the mySQL connection because I didn't have the need for any other connections.

    Each of the 3 Execute methods has 2 overloads; one accepting a commandString, and one that accepts a commandString and a dictionary(of String, Object). The dictionary contains parameter values for the command and the key/value pairs are set using ("@fieldName",columnValue) format. The dictionary keys must match the placeholders used in the commandString. To make this easier, there is a method that will accept a Dictionary of parameters that will return a partial commandString that can be appended to the commandString passed to the Execute method. Using this method ensures synchronicity between the commandString and the parameter keys.

    With the intent of being namespace independent, the ExecuteReader method which would normally return a DataReader, returns a DataTable. DataReaders are namespace dependent, whereas Datatables are not.

    The class also has an InsertWithID method (also with 2 overloads). This method will insert a row and return its ID by first inserting the row, then by issuing a "SELECT_LAST_INSERT_ID" command on the database.

    There are a number of other helper method included as well; doesTableExist, dataBaseSize, getMaxFromColumns, getColumnNames


    This class has served me well of the past 2-3 years, and I use it exclusively for all of my database connections. Maybe some I'll create the interface and extend this to other databases, but for now, it only implements the MySQL namespace so you need to have the MySQL connector installed and the class library needs to reference it.
    Enjoy.
    Mickabooh.SQL.zip
    Last edited by kebo; Feb 5th, 2017 at 09:21 AM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width