Ejemplo n.º 1
0
        public object ReturnObject(Hashtable Params, string CommandText, ClsDBUtility.ObjectEnum Obj, DataTable theDataTable, string tablename)
        {
            int                i;
            string             cmdpara, cmdvalue, cmddbtype;
            ParameterDirection cmdDirection;
            SqlCommand         theCmd   = new SqlCommand();
            SqlTransaction     theTran  = (SqlTransaction)this.Transaction;
            SqlConnection      cnn      = null;
            StringBuilder      strParam = new StringBuilder();

            try
            {
                if (null == this.Connection)
                {
                    cnn = (SqlConnection)DataMgr.GetConnection();
                }
                else
                {
                    cnn = (SqlConnection)this.Connection;
                }

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

                for (i = 1; i < Params.Count;)
                {
                    cmdpara   = Params[i].ToString();
                    cmddbtype = Params[i + 1].ToString();
                    if (Params[i + 2] != null)
                    {
                        if (Params[i + 2].GetType() != ParameterDirection.Output.GetType())
                        {
                            cmdvalue = Params[i + 2].ToString();
                            theCmd.Parameters.AddWithValue(cmdpara, cmddbtype).Value = cmdvalue;
                        }
                        else
                        {
                            cmdDirection = (ParameterDirection)Params[i + 2];
                            theCmd.Parameters.AddWithValue(cmdpara, (SqlDbType)Params[i + 1]).Direction = cmdDirection;
                        }
                    }
                    else
                    {
                        cmdvalue = string.Empty;
                        theCmd.Parameters.AddWithValue(cmdpara, cmddbtype).Value = cmdvalue;
                    }
                    i = i + 3;
                }
                if (theDataTable != null)
                {
                    if (theDataTable.Rows.Count > 0)
                    {
                        theCmd.Parameters.AddWithValue(tablename, theDataTable);
                    }
                }

                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)
                {
                    SqlDataAdapter theAdpt = new SqlDataAdapter(theCmd);
                    DataSet        theDS   = new DataSet();
                    theAdpt.Fill(theDS);
                    theAdpt.Dispose();
                    return(theDS);
                }

                if (Obj == ClsDBUtility.ObjectEnum.DataTable)
                {
                    SqlDataAdapter theAdpt = new SqlDataAdapter(theCmd);
                    DataTable      theDT   = new DataTable();
                    theAdpt.Fill(theDT);
                    theAdpt.Dispose();
                    return(theDT);
                }

                if (Obj == ClsDBUtility.ObjectEnum.DataRow)
                {
                    SqlDataAdapter theAdpt = new SqlDataAdapter(theCmd);
                    DataTable      theDT   = new DataTable();
                    theAdpt.Fill(theDT);
                    theAdpt.Dispose();
                    return(theDT.Rows[0]);
                }


                if (Obj == ClsDBUtility.ObjectEnum.ExecuteNonQuery)
                {
                    int NoRowsAffected = 0;
                    NoRowsAffected = theCmd.ExecuteNonQuery();
                    if (theCmd.Parameters.Contains("@idNEW"))
                    {
                        if ((int)theCmd.Parameters["@idNEW"].Value > 0)
                        {
                            NoRowsAffected = (int)theCmd.Parameters["@idNEW"].Value;
                        }
                    }

                    return(NoRowsAffected);
                }
                return(0);
            }
            catch (Exception err)
            {
                for (i = 1; i < Params.Count;)
                {
                    cmdpara   = Params[i].ToString();
                    cmddbtype = Params[i + 1].ToString();
                    if (Params[i + 2] != null)
                    {
                        if (Params[i + 2].GetType() != ParameterDirection.Output.GetType())
                        {
                            cmdvalue = Params[i + 2].ToString();
                            strParam.Append("Name: " + cmdpara + ", Type: " + cmddbtype + ", Value: " + cmdvalue + ", Direction: Input");
                            strParam.Append(Environment.NewLine);
                        }
                        else
                        {
                            cmdDirection = (ParameterDirection)Params[i + 2];
                            strParam.Append("Name: " + cmdpara + ", Type: " + cmddbtype + ", Direction: Output");
                            strParam.Append(Environment.NewLine);
                        }
                    }
                    i = i + 3;
                }

                CLogger.WriteLog("Namespace: DataAccess.Entity, Class: ClsObject, Method: ReturnObject - Call started.", CommandText, strParam.ToString(), err.ToString());

                throw err;
            }
            finally
            {
                if (null != cnn)
                {
                    if (null == this.Connection)
                    {
                        DataMgr.ReleaseConnection(cnn);
                    }
                }
            }
        }
Ejemplo n.º 2
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);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public object ReturnObject(Hashtable Params, string CommandText, ClsDBUtility.ObjectEnum Obj, bool IQTools)
        {
            int            i;
            string         cmdpara, cmddbtype;
            object         cmdvalue;
            SqlCommand     theCmd  = new SqlCommand();
            SqlTransaction theTran = (SqlTransaction)this.Transaction;
            SqlConnection  cnn;

            if (null == this.Connection)
            {
                cnn = (SqlConnection)DataMgr.GetConnection();
            }
            else
            {
                cnn = (SqlConnection)this.Connection;
            }

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

            for (i = 1; i < Params.Count;)
            {
                cmdpara   = Params[i].ToString();
                cmddbtype = Params[i + 1].ToString();
                //if (cmddbtype.Contains("binary"))
                cmdvalue = Params[i + 2];
                //else
                //    cmdvalue = Params[i + 2].ToString();
                // theCmd.Parameters.Add(cmdpara, cmddbtype).Value = cmdvalue;
                theCmd.Parameters.AddWithValue(cmdpara, cmdvalue);//.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;

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

            theCmd.Connection = cnn;
            try
            {
                if (Obj == ClsDBUtility.ObjectEnum.DataSet)
                {
                    SqlDataAdapter theAdpt = new SqlDataAdapter(theCmd);
                    DataSet        theDS   = new DataSet();
                    theAdpt.Fill(theDS);
                    theAdpt.Dispose();
                    return(theDS);
                }

                if (Obj == ClsDBUtility.ObjectEnum.DataTable)
                {
                    SqlDataAdapter theAdpt = new SqlDataAdapter(theCmd);
                    DataTable      theDT   = new DataTable();
                    theAdpt.Fill(theDT);
                    theAdpt.Dispose();
                    return(theDT);
                }

                if (Obj == ClsDBUtility.ObjectEnum.DataRow)
                {
                    SqlDataAdapter theAdpt = new SqlDataAdapter(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)
            {
                throw err;
            }
            finally
            {
                if (null != cnn)
                {
                    if (null == this.Connection)
                    {
                        DataMgr.ReleaseConnection(cnn);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns the object.
        /// </summary>
        /// <param name="paramaters">The parameters.</param>
        /// <param name="commandText">The command text.</param>
        /// <param name="Obj">The object.</param>
        /// <param name="Mode">The mode.</param>
        /// <returns></returns>
        public object ReturnObject(Hashtable paramaters, string commandText, ClsUtility.ObjectEnum Obj, ConnectionMode Mode)
        {
            //int i;
            //string cmdpara, cmdvalue, cmddbtype;
            SqlCommand     theCmd  = new SqlCommand();
            SqlTransaction theTran = (SqlTransaction)this.Transaction;
            SqlConnection  cnn;

            if (null == this.Connection)
            {
                cnn = (SqlConnection)DataMgr.GetConnection(Mode);
            }
            else
            {
                cnn = (SqlConnection)this.Connection;
            }
            if (null == this.Transaction)
            {
                theCmd = new SqlCommand(commandText, cnn);
            }
            else
            {
                theCmd = new SqlCommand(commandText, cnn, theTran);
            }

            /* for (i = 1; i < Params.Count; )
             * {
             *   cmdpara = Params[i].ToString();
             *   cmddbtype = Params[i + 1].ToString();
             *   cmdvalue = Params[i + 2].ToString();
             *   System.Data.Common.DbParameter p = theCmd.CreateParameter();
             *   p.ParameterName = cmdpara;
             *   p.Value = cmdvalue;
             *   theCmd.Parameters.Add(cmdpara);
             *
             *   theCmd.Parameters.Add(cmdpara, cmddbtype).Value = cmdvalue;
             *   i = i + 3;
             * }
             */
            for (int i = 1; i <= paramaters.Count;)
            {
                theCmd.Parameters.Add(paramaters[i]);
                i++;
            }
            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;

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

            theCmd.Connection = cnn;
            try
            {
                if (Obj == ClsUtility.ObjectEnum.DataSet)
                {
                    SqlDataAdapter theAdpt = new SqlDataAdapter(theCmd);
                    DataSet        theDS   = new DataSet();
                    theAdpt.Fill(theDS);
                    theAdpt.Dispose();
                    return(theDS);
                }

                if (Obj == ClsUtility.ObjectEnum.DataTable)
                {
                    SqlDataAdapter theAdpt = new SqlDataAdapter(theCmd);
                    DataTable      theDT   = new DataTable();
                    theAdpt.Fill(theDT);
                    theAdpt.Dispose();
                    return(theDT);
                }

                if (Obj == ClsUtility.ObjectEnum.DataRow)
                {
                    SqlDataAdapter theAdpt = new SqlDataAdapter(theCmd);
                    DataTable      theDT   = new DataTable();
                    theAdpt.Fill(theDT);
                    theAdpt.Dispose();
                    return(theDT.Rows[0]);
                }


                if (Obj == ClsUtility.ObjectEnum.ExecuteNonQuery)
                {
                    int NoRowsAffected = theCmd.ExecuteNonQuery();
                    return(NoRowsAffected);
                }
                return(0);
            }
            catch (Exception err)
            {
                throw err;
            }
            finally
            {
                if (null != cnn)
                {
                    if (null == this.Connection)
                    {
                        DataMgr.ReleaseConnection(cnn);
                    }
                }
            }
        }