Yehonatan

C# asp.net MysqlWrapper

Nov 10th, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using MySql.Data.MySqlClient;
  6. using System.Collections;
  7. using System.Diagnostics;
  8. using System.Data;
  9.  
  10. namespace MvcApplication1.Models
  11. {
  12.     public class MysqlWrapper
  13.     {
  14.         private MySqlConnection _connection;
  15.         private string _connectionString = "";
  16.         private string _query;
  17.         private MySqlDataReader _currentRead;
  18.  
  19.     /*
  20.     *This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
  21.         * To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/.
  22.     * Copyrights resevred.
  23.     */
  24.    
  25.         public MysqlWrapper(string server, string database, string user, string password)
  26.         {
  27.            _connectionString = "server=" + server +";database=" + database +";uid=" + user + ";password=" + password;
  28.            try
  29.            {
  30.                 _connection = new MySqlConnection(_connectionString);
  31.                 _connection.Open();
  32.            }
  33.             catch(Exception excp)
  34.             {
  35.                 Exception myExcp = new Exception("Error connecting to MySql server. Internal error message: " + excp.Message, excp);
  36.                 throw myExcp;
  37.             }
  38.            
  39.         }
  40.  
  41.           //Convert to OrderedDictonery
  42.         public MysqlWrapper(string[] connection_data)
  43.           {
  44.               _connectionString = "server=" + connection_data[0] + ";database=" + connection_data[1] + ";uid=" + connection_data[2] + ";password=" + connection_data[3];
  45.               try
  46.               {
  47.                   _connection = new MySqlConnection(_connectionString);
  48.                   _connection.Open();
  49.               }
  50.               catch (Exception excp)
  51.               {
  52.                   Exception myExcp = new Exception("Error connecting to MySql server. Internal error message: " + excp.Message, excp);
  53.                   throw myExcp;
  54.               }
  55.  
  56.           }
  57.  
  58.           public MysqlWrapper(Hashtable connection_data)
  59.         {
  60.             _connectionString = "server=" + connection_data.ContainsKey("server").ToString() + ";database="
  61.                 + connection_data.ContainsKey("database").ToString()
  62.                 + ";uid=" + connection_data.ContainsKey("user").ToString() + ";password=" + connection_data.ContainsKey("password").ToString();
  63.            
  64.             try
  65.             {
  66.                 _connection = new MySqlConnection(_connectionString);
  67.                 _connection.Open();
  68.             }
  69.             catch (Exception excp)
  70.             {
  71.                 Exception myExcp = new Exception("Error connecting to MySql server. Internal error message: " + excp.Message, excp);
  72.                 throw myExcp;
  73.             }
  74.         }
  75.  
  76.         public MysqlWrapper(string connection_data)
  77.         {
  78.             _connectionString = connection_data;
  79.             try
  80.             {
  81.                  this._connection = new MySqlConnection(_connectionString);
  82.                  this._connection.Open();
  83.             }
  84.             catch (Exception excp)
  85.             {
  86.                 Exception myExcp = new Exception("Error connecting to MySql server. Internal error message: " + excp.Message, excp);
  87.                 throw myExcp;
  88.             }
  89.         }
  90.  
  91.  
  92.           public bool Query(string query)
  93.           {
  94.               MySqlCommand q = new MySqlCommand(query, this._connection);
  95.               try
  96.               {
  97.                   q.ExecuteNonQuery();
  98.                   return true;
  99.               }
  100.               catch (Exception excp)
  101.               {
  102.                   Exception myExcp = new Exception("Could not add user. Error: " +
  103.                       excp.Message, excp);
  104.                   return false;
  105.                   throw (myExcp);
  106.               }
  107.           }
  108.            
  109.         private void revive(string connection_string)
  110.         {
  111.             try
  112.             {
  113.                 _connection = new MySqlConnection(connection_string);
  114.                 _connection.Open();
  115.             }
  116.             catch (Exception excp)
  117.             {
  118.                 Exception myExcp = new Exception("Error connecting to MySql server. Internal error message: " + excp.Message, excp);
  119.                 throw myExcp;
  120.             }
  121.         }
  122.  
  123.           public bool Select(string query)
  124.           {
  125.  
  126.              this.revive(_connectionString);
  127.              _query = query;
  128.  
  129.               MySqlCommand q = new MySqlCommand(query, this._connection);
  130.               q.Connection = _connection;
  131.               q.CommandType = CommandType.Text;
  132.               q.CommandText = query;
  133.  
  134.               try
  135.               {
  136.                  // q.ExecuteNonQuery();
  137.                  _currentRead = q.ExecuteReader(System.Data.CommandBehavior.Default);
  138.                   return true;
  139.               }
  140.               catch (Exception excp)
  141.               {
  142.                   Exception myExcp = new Exception("Could not use SELECT. Error: " +
  143.                       excp.Message, excp);
  144.                   throw (myExcp);
  145.               }
  146.           }
  147.  
  148.         //Or using string array with cloning or sortedList/Set
  149.           public string[] getStr(int table_index)
  150.           {
  151.               List<string> items = new List<string>();
  152.  
  153.               while (this._currentRead.Read())
  154.               {
  155.                   items.Add(this._currentRead.GetString(table_index));
  156.               }
  157.  
  158.               string[] retrun_items = new string[items.Count];
  159.  
  160.               for (int i = 0; i < items.Count; i++)
  161.                   retrun_items[i] = items.ElementAt(i);
  162.  
  163.               this._currentRead = null;
  164.  
  165.               Debug.WriteLine(_query + "$!$");
  166.               this.Select(_query);
  167.  
  168.               return retrun_items;
  169.  
  170.           }
  171.  
  172.           public int[] getInt(int table_index, int type)
  173.           {
  174.               List<int> items_list = new List<int>();
  175.  
  176.               while (_currentRead.Read())
  177.               {
  178.                   switch(type)
  179.                   {
  180.                       case 16:
  181.                           items_list.Add(_currentRead.GetInt16(table_index));
  182.                           break;
  183.                       case 32:
  184.                           items_list.Add(_currentRead.GetInt32(table_index));
  185.                           break;
  186.                       case 64:
  187.                           items_list.Add((int)_currentRead.GetInt64(table_index));
  188.                           break;
  189.                       default:
  190.                           items_list.Add(_currentRead.GetInt32(table_index));
  191.                       break;
  192.  
  193.                   }
  194.               }
  195.  
  196.               int[] items = new int[items_list.Count];
  197.  
  198.               for (int i = 0; i < items_list.Count; i++)
  199.                   items[i] = items_list.ElementAt(i);
  200.  
  201.               return items;
  202.  
  203.           }
  204.  
  205.           public int countQuery(string what, string from, string additional)
  206.           {
  207.               this.revive(_connectionString);
  208.               string query = "SELECT COUNT(" + what + ") FROM `" + from + "`" + additional;
  209.  
  210.               MySqlCommand q = new MySqlCommand(query, this._connection);
  211.  
  212.               try
  213.               {
  214.  
  215.                   return Convert.ToInt32(q.ExecuteScalar().ToString());
  216.               }
  217.               catch (Exception excp)
  218.               {
  219.                   Exception myExcp = new Exception("Could not use SELECT. Error: " +
  220.                       excp.Message, excp);
  221.                   throw (myExcp);
  222.               }
  223.  
  224.  
  225.           }
  226.  
  227.           public string protectString(string str)
  228.           {
  229.               str = str.Replace("'", "&#39;");
  230.               str = str.Replace("\"", "&#34;");
  231.               str = str.Replace("#", "&#35;");
  232.               str = str.Replace("-", "&#45;");
  233.               return str;
  234.           }
  235.  
  236.  
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment