Example #1
0
        public object ReturnMySQLObject(Hashtable Params, string CommandText, ClsDBUtility.ObjectEnum Obj)
        {
            int              i;
            string           cmdpara, cmdvalue, cmddbtype;
            MySqlCommand     theCmd   = new MySqlCommand();
            MySqlTransaction theTran  = (MySqlTransaction)this.Transaction;
            MySqlConnection  cnn      = null;
            StringBuilder    strParam = new StringBuilder();

            try
            {
                if (null == this.Connection)
                {
                    cnn = (MySqlConnection)DataMgr.GetMySQLConnection();
                }
                else
                {
                    cnn = (MySqlConnection)this.Connection;
                }

                if (null == this.Transaction)
                {
                    theCmd = new MySqlCommand(CommandText, cnn);
                }
                else
                {
                    theCmd = new MySqlCommand(CommandText, cnn, theTran);
                }

                for (i = 1; i < Params.Count;)
                {
                    cmdpara   = Params[i].ToString();
                    cmddbtype = Params[i + 1].ToString();
                    cmdvalue  = Params[i + 2].ToString();
                    theCmd.Parameters.Add(cmdpara, cmddbtype).Value = cmdvalue;
                    i = i + 3;
                }

                theCmd.CommandType    = CommandType.StoredProcedure;
                theCmd.CommandTimeout = DataMgr.CommandTimeOut();
                string theSubstring = CommandText.Substring(0, 6).ToUpper();
                switch (theSubstring)
                {
                case "SELECT":
                    theCmd.CommandType = CommandType.Text;
                    break;

                case "UPDATE":
                    theCmd.CommandType = CommandType.Text;
                    break;

                case "INSERT":
                    theCmd.CommandType = CommandType.Text;
                    break;

                case "DELETE":
                    theCmd.CommandType = CommandType.Text;
                    break;
                }

                theCmd.Connection = cnn;

                if (Obj == ClsDBUtility.ObjectEnum.DataSet)
                {
                    MySqlDataAdapter theAdpt = new MySqlDataAdapter(theCmd);
                    DataSet          theDS   = new DataSet();
                    theAdpt.Fill(theDS);
                    theAdpt.Dispose();
                    return(theDS);
                }
                if (Obj == ClsDBUtility.ObjectEnum.DataTable)
                {
                    MySqlDataAdapter theAdpt = new MySqlDataAdapter(theCmd);
                    DataTable        theDT   = new DataTable();
                    theAdpt.Fill(theDT);
                    theAdpt.Dispose();
                    return(theDT);
                }
                if (Obj == ClsDBUtility.ObjectEnum.DataRow)
                {
                    MySqlDataAdapter theAdpt = new MySqlDataAdapter(theCmd);
                    DataTable        theDT   = new DataTable();
                    theAdpt.Fill(theDT);
                    theAdpt.Dispose();
                    return(theDT.Rows[0]);
                }
                if (Obj == ClsDBUtility.ObjectEnum.ExecuteNonQuery)
                {
                    int NoRowsAffected = theCmd.ExecuteNonQuery();
                    return(NoRowsAffected);
                }
                return(0);
            }
            catch (Exception err)
            {
                for (i = 1; i < Params.Count;)
                {
                    cmdpara   = Params[i].ToString();
                    cmddbtype = Params[i + 1].ToString();
                    cmdvalue  = Params[i + 2].ToString();
                    strParam.Append("Name: " + cmdpara + ", Type: " + cmddbtype + ", Value: " + cmdvalue + " ");
                    strParam.Append(Environment.NewLine);

                    i = i + 3;
                }
                CLogger.WriteLog("Namespace: DataAccess.Entity, Class: ClsObject, Method: ReturnMySQLObject - Call started.", CommandText, strParam.ToString(), err.ToString());
                throw new ApplicationException("Either MySQL configuration missing or connection String not properly set", err);
                //throw err;
            }
            finally
            {
                if (null != cnn)
                {
                    if (null == this.Connection)
                    {
                        DataMgr.ReleaseMySQLConnection(cnn);
                    }
                }
            }
        }