Ortund

First ever overloaded function

May 28th, 2013
177
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using MySql.Data.MySqlClient;
  2. using System.Data;
  3.  
  4. public class MySqlContext : IDisposable
  5. {
  6.     private MySqlConnection conn;
  7.  
  8.     private string serverAddress = "127.0.0.1";
  9.     private string database = "dbtest";
  10.     private string user = "root";
  11.     private string password = "root";
  12.  
  13.     public MySqlContext()
  14.     {
  15.         string strConnection = String.Format("Data Source={0}; Initial Catalog={1}; User ID={2}; Password={3};", serverAddress, database, user, password);
  16.  
  17.         conn = new MySqlConnection(strConnection);
  18.     }
  19.  
  20.     public MySqlDataReader GetReader(string sql, List<MySqlParameter> args)
  21.     {
  22.         if ((args == null) || (args.Count == 0))
  23.         {
  24.             return GetReader(sql);
  25.         }
  26.         else
  27.         {
  28.             return GetReader(sql, args.ToArray());
  29.         }
  30.     }
  31.  
  32.     public MySqlDataReader GetReader(string sql, MySqlParameter[] args = null)
  33.     {
  34.         MySqlCommand cmd = new MySqlCommand(sql, conn);
  35.         cmd.CommandType = System.Data.CommandType.Text;
  36.  
  37.         if (conn.State == ConnectionState.Close)
  38.         {
  39.             conn.Open();
  40.         }
  41.  
  42.         if (args != null)
  43.         {
  44.             cmd.Parameters.AddRange(args);
  45.         }
  46.  
  47.         MySqlDataReader dr = cmd.ExecuteReader();
  48.  
  49.         return dr;
  50.     }
  51.  
  52.     public void Dispose()
  53.     {
  54.         conn.Close();
  55.         conn.Dispose();
  56.     }
  57. }
RAW Paste Data