Esempio n. 1
0
        public static ResultArgs FetchData <T>(OracleParameter[] param, string sSQL, EnumCommand.DataSource dataSourceType, string sAcademicYear = "") where T : new()
        {
            ResultArgs resultArgs = new ResultArgs();
            DataValue  dv         = new DataValue();

            dv.Clear();
            try
            {
                if (string.IsNullOrEmpty(sSQL))
                {
                    resultArgs.Success = false;
                    resultArgs.Message = Common.Message.QueryIsEmpty;
                    return(resultArgs);
                }
                if (sAcademicYear != string.Empty)
                {
                    sSQL = sSQL.Replace(Common.Delimiter.QUS + "", sAcademicYear);
                }
                //if (param != null)
                //{
                //    DataValue Construction
                //    foreach (var prop in obj.GetType().GetProperties())
                //    {
                //        objValue = prop.GetValue(obj, null);
                //        objValue = (objValue == null) ? string.Empty : objValue.ToString();
                //        dv.Add(prop.Name, objValue.ToString(), EnumCommand.DataType.String);
                //    }
                //}
                if (dataSourceType == EnumCommand.DataSource.list)
                {
                    using (OracleHelper objHelper = new OracleHelper())
                    {
                        resultArgs = objHelper.FecthDataAsList <T>(sSQL, param, EnumCommand.SQLType.StoredProcedure);
                    }
                }
                else
                {
                    using (OracleHelper objHelper = new OracleHelper())
                    {
                        resultArgs = objHelper.FetchData(sSQL, dataSourceType, param, EnumCommand.SQLType.StoredProcedure);
                    }
                }
            }
            catch (System.Exception ex)
            {
                using (ErrorLog objlog = new ErrorLog())
                {
                    objlog.WriteError("OracleHelper", "FetchData", sSQL, ex.Message);
                }
                resultArgs.Success = false;
            }
            return(resultArgs);
        }
Esempio n. 2
0
        /// <summary>Execute the Select Query  </summary>
        /// <param name="dv">DataValue Object</param>
        /// <param name="query">The Select Query</param>
        /// <returns>DataTable with the records</returns>
        /// <exception name="ArgumentNullException"></exception>
        /// <exception name="InvalidOperationException"></exception>
        public ResultArgs FetchData(string query, EnumCommand.DataSource dataSourceType, EnumCommand.SQLType sqlType = EnumCommand.SQLType.Static, DataValue dv = null)
        {
            ResultArgs result = new ResultArgs();

            result.Success = false;
            SqlDataAdapter Adapter;
            SqlCommand     Command = null;
            DataSet        ds      = new DataSet();
            SqlConnection  Con     = OpenConnection();

            try
            {
                if (string.IsNullOrEmpty(query))
                {
                    //throw new ArgumentNullException("Query is empty", "FetchData(DataValue dv, string query)");
                    using (ErrorLog objlog = new ErrorLog())
                    {
                        result.Success = false;
                        objlog.WriteError("Error Handler", "FetchData(DataValue,string)", query, "Query is empty!");
                    }
                }
                else
                {
                    if (dv == null)
                    {
                        Command = new SqlCommand(query, Con);
                    }
                    else
                    {
                        Command = SetSQLCommand(dv, query, Con, sqlType);
                    }

                    result.dataSource = null;
                    try
                    {
                        if (Con.State == ConnectionState.Open && Command != null)
                        {
                            switch (dataSourceType)
                            {
                            case EnumCommand.DataSource.DataSet:
                            {
                                Adapter = new SqlDataAdapter(Command);
                                if (result.dataSource == null)
                                {
                                    result.dataSource = new DataSet();
                                }
                                result.RowsAffected = Adapter.Fill(result.dataSource as DataSet);
                                result.Success      = true;
                                break;
                            }

                            case EnumCommand.DataSource.DataView:
                            {
                                Adapter = new SqlDataAdapter(Command);
                                if (result.dataSource == null)
                                {
                                    result.dataSource = new DataTable();
                                }
                                result.RowsAffected = Adapter.Fill(result.dataSource as DataTable);
                                result.dataSource   = ((DataTable)result.dataSource).DefaultView;
                                result.Success      = true;
                                break;
                            }

                            case EnumCommand.DataSource.DataReader:
                            {
                                result.dataSource   = Command.ExecuteReader();
                                result.RowsAffected = ((SqlDataReader)result.dataSource).RecordsAffected;
                                result.Success      = true;
                                break;
                            }

                            case EnumCommand.DataSource.Scalar:
                            {
                                result.dataSource = Command.ExecuteScalar();
                                if (result.dataSource != null)
                                {
                                    result.RowsAffected = 1;
                                    result.Success      = true;
                                }
                                else
                                {
                                    result.RowsAffected = 0;
                                    result.Success      = true;
                                }

                                break;
                            }

                            default:
                            {
                                Adapter = new SqlDataAdapter(Command);
                                if (result.dataSource == null)
                                {
                                    result.dataSource = new DataTable();
                                }
                                result.RowsAffected = Adapter.Fill(result.dataSource as DataTable);
                                result.Success      = true;
                                break;
                            }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        //Update Exception
                        result.Exception = e;
                        result.Success   = false;
                        using (ErrorLog objlog = new ErrorLog())
                        {
                            objlog.WriteError("Error Handler", "FetchData(DataValue,string)", query, e.Message + " Query is empty!");
                        }
                        //new ErrorLog().WriteError("MySQLDataHandler", "Fetch", e.Message, Command.CommandText, CommonMethods.GetExceptionLineNo(e));
                    }
                    finally
                    {
                        // CloseConnection(TransactionType.None);
                        CloseConnection();
                    }

                    //result.ShowMessage("Updated");
                    if (result.Success)
                    {
                        result.DataSource.Data = result.dataSource;
                    }
                    //Adapter = new SqlDataAdapter(Command);
                    //Adapter.Fill(ds);
                }
            }
            catch (Exception ex)
            {
                result.Success = false;
                using (ErrorLog objHandler = new ErrorLog())
                {
                    objHandler.WriteError("MysqlHandler", "FetchData", ex.Message);
                }
            }
            finally
            {
                if (Command != null)
                {
                    Command.Dispose();
                }
                Command = null;
                CloseConnection();
            }
            return(result);
        }