Using System;
Using System.Collections.Generic;
Using System.Linq;
Using system.text;//Guide namespaces
Using System.Data;
Using MySql.Data.MySqlClient;
Using System.Configuration;
Namespace TEST.DAL.MYSQL
{
public class Dbhelp {private readonly string connstring = configurationmanager.connectionstrings["MySQL"]. ToString (); Private mysqlconnection mysqlconnection = null; Private Mysqlcommand Mysqlcommand = null; protected Mysqldatareader mysqldatareader = null; Private Mysqldataadapter mysqldataadapter = null; Private DataSet DataSet = null; private int i = 0; Private Mysqlconnection executemysqlconnection () {try {this.mysqlconnection = new mysqlconnection (this.connstring); This.mySqlConnection.Open (); } catch (Exception) {throw;} return this.mysqlconnection; } Private Mysqlcommand Executemysqlcommand (String sql,commandtype commandtype,mysqlparameter[] mysqlparameter) {try { This.mysqlconnection = this. Executemysqlconnection (); This.mysqlcommand = new Mysqlcommand (sql, this.mysqlconnection); if (mysqlparameter! = null && mysqlparameter.length > 0) {this.mySqlCommand.Parameters.AddRange ( Mysqlparameter); } This.mySqlCommand.CommandType = CommandType; } catch (Exception) {THrow; } return This.mysqlcommand; } protected Mysqldatareader Executemysqldatareader (String sql, CommandType commandtype, mysqlparameter[] Mysqlparameter) {try {This.mysqlcommand = this. Executemysqlcommand (SQL, CommandType, Mysqlparameter); This.mysqldatareader = This.mySqlCommand.ExecuteReader (); } catch (Exception) {throw;} return this.mysqldatareader; } protected DataSet ExecuteDataset (String sql, CommandType commandtype, mysqlparameter[] mysqlparameter) {This.dataset = New DataSet (); try {This.mysqlcommand = this. Executemysqlcommand (SQL, CommandType, Mysqlparameter); This.mysqldataadapter = new Mysqldataadapter (This.mysqlcommand); This.mySqlDataAdapter.Fill (DataSet); } catch (Exception) {throw;} return this.dataset; } protected int ExecuteScalar (String sql, CommandType commandtype, mysqlparameter[] mysqlparameter) {try {This.mysqlcomm and = this. Executemysqlcommand (SQL, CommandType, Mysqlparameter); this.i = Int. Parse (This.mySqlCommand.ExecuteScalar (). ToString ()); } catch(Exception) {throw;} return this.i; } protected int ExecuteNonQuery (String sql, CommandType commandtype, mysqlparameter[] mysqlparameter) {try {This.mysqlco Mmand = this. Executemysqlcommand (SQL, CommandType, Mysqlparameter); THIS.I = This.mySqlCommand.ExecuteNonQuery (); } catch (Exception) {throw;} return this.i; } protected void CloseConnection (Mysqlconnection mysqlconnection) {try {if (mysqlconnection! = null) {mysqlconnection = Null Mysqlconnection.close (); }} catch (Exception) {throw;}} protected void Closedatareader (Mysqldatareader mysqldatareader) {try {if (Mysqldatareader! = null) {Mysqldatareader = n Ull Mysqldatareader.close (); }} catch (Exception) {throw;}} }
}
MYSQL C # General Class