Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using MySql.Data.MySqlClient;
- using System.Collections;
- using System.Diagnostics;
- using System.Data;
- namespace MvcApplication1.Models
- {
- public class MysqlWrapper
- {
- private MySqlConnection _connection;
- private string _connectionString = "";
- private string _query;
- private MySqlDataReader _currentRead;
- /*
- *This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
- * To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/.
- * Copyrights resevred.
- */
- public MysqlWrapper(string server, string database, string user, string password)
- {
- _connectionString = "server=" + server +";database=" + database +";uid=" + user + ";password=" + password;
- try
- {
- _connection = new MySqlConnection(_connectionString);
- _connection.Open();
- }
- catch(Exception excp)
- {
- Exception myExcp = new Exception("Error connecting to MySql server. Internal error message: " + excp.Message, excp);
- throw myExcp;
- }
- }
- //Convert to OrderedDictonery
- public MysqlWrapper(string[] connection_data)
- {
- _connectionString = "server=" + connection_data[0] + ";database=" + connection_data[1] + ";uid=" + connection_data[2] + ";password=" + connection_data[3];
- try
- {
- _connection = new MySqlConnection(_connectionString);
- _connection.Open();
- }
- catch (Exception excp)
- {
- Exception myExcp = new Exception("Error connecting to MySql server. Internal error message: " + excp.Message, excp);
- throw myExcp;
- }
- }
- public MysqlWrapper(Hashtable connection_data)
- {
- _connectionString = "server=" + connection_data.ContainsKey("server").ToString() + ";database="
- + connection_data.ContainsKey("database").ToString()
- + ";uid=" + connection_data.ContainsKey("user").ToString() + ";password=" + connection_data.ContainsKey("password").ToString();
- try
- {
- _connection = new MySqlConnection(_connectionString);
- _connection.Open();
- }
- catch (Exception excp)
- {
- Exception myExcp = new Exception("Error connecting to MySql server. Internal error message: " + excp.Message, excp);
- throw myExcp;
- }
- }
- public MysqlWrapper(string connection_data)
- {
- _connectionString = connection_data;
- try
- {
- this._connection = new MySqlConnection(_connectionString);
- this._connection.Open();
- }
- catch (Exception excp)
- {
- Exception myExcp = new Exception("Error connecting to MySql server. Internal error message: " + excp.Message, excp);
- throw myExcp;
- }
- }
- public bool Query(string query)
- {
- MySqlCommand q = new MySqlCommand(query, this._connection);
- try
- {
- q.ExecuteNonQuery();
- return true;
- }
- catch (Exception excp)
- {
- Exception myExcp = new Exception("Could not add user. Error: " +
- excp.Message, excp);
- return false;
- throw (myExcp);
- }
- }
- private void revive(string connection_string)
- {
- try
- {
- _connection = new MySqlConnection(connection_string);
- _connection.Open();
- }
- catch (Exception excp)
- {
- Exception myExcp = new Exception("Error connecting to MySql server. Internal error message: " + excp.Message, excp);
- throw myExcp;
- }
- }
- public bool Select(string query)
- {
- this.revive(_connectionString);
- _query = query;
- MySqlCommand q = new MySqlCommand(query, this._connection);
- q.Connection = _connection;
- q.CommandType = CommandType.Text;
- q.CommandText = query;
- try
- {
- // q.ExecuteNonQuery();
- _currentRead = q.ExecuteReader(System.Data.CommandBehavior.Default);
- return true;
- }
- catch (Exception excp)
- {
- Exception myExcp = new Exception("Could not use SELECT. Error: " +
- excp.Message, excp);
- throw (myExcp);
- }
- }
- //Or using string array with cloning or sortedList/Set
- public string[] getStr(int table_index)
- {
- List<string> items = new List<string>();
- while (this._currentRead.Read())
- {
- items.Add(this._currentRead.GetString(table_index));
- }
- string[] retrun_items = new string[items.Count];
- for (int i = 0; i < items.Count; i++)
- retrun_items[i] = items.ElementAt(i);
- this._currentRead = null;
- Debug.WriteLine(_query + "$!$");
- this.Select(_query);
- return retrun_items;
- }
- public int[] getInt(int table_index, int type)
- {
- List<int> items_list = new List<int>();
- while (_currentRead.Read())
- {
- switch(type)
- {
- case 16:
- items_list.Add(_currentRead.GetInt16(table_index));
- break;
- case 32:
- items_list.Add(_currentRead.GetInt32(table_index));
- break;
- case 64:
- items_list.Add((int)_currentRead.GetInt64(table_index));
- break;
- default:
- items_list.Add(_currentRead.GetInt32(table_index));
- break;
- }
- }
- int[] items = new int[items_list.Count];
- for (int i = 0; i < items_list.Count; i++)
- items[i] = items_list.ElementAt(i);
- return items;
- }
- public int countQuery(string what, string from, string additional)
- {
- this.revive(_connectionString);
- string query = "SELECT COUNT(" + what + ") FROM `" + from + "`" + additional;
- MySqlCommand q = new MySqlCommand(query, this._connection);
- try
- {
- return Convert.ToInt32(q.ExecuteScalar().ToString());
- }
- catch (Exception excp)
- {
- Exception myExcp = new Exception("Could not use SELECT. Error: " +
- excp.Message, excp);
- throw (myExcp);
- }
- }
- public string protectString(string str)
- {
- str = str.Replace("'", "'");
- str = str.Replace("\"", """);
- str = str.Replace("#", "#");
- str = str.Replace("-", "-");
- return str;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment