Esempio n. 1
0
        public DatabaseSession GetDatabaseSession(string connectionString)
        {
            string FUNCTION_NAME = "GetDatabaseSession";

            LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                   EDebugLevelManaged.DebugInfo, "Function_Entered");

            if (connectionString != null)
            {
                DatabaseSession session = m_sessionStore.GetSession(connectionString);
                if (session == null)
                {
                    CreateDatabaseSession(connectionString);
                    session = m_sessionStore.GetSession(connectionString);
                }
                m_sessionStore.StoreCurrentSession(session);

                LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                       EDebugLevelManaged.DebugInfo, "Function_Exited");
                return(session);
            }

            LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                   EDebugLevelManaged.DebugInfo, "Function_Exited");
            return(null);
        }
Esempio n. 2
0
        public DatabaseSession GetDatabaseSession()
        {
            string FUNCTION_NAME = "GetDatabaseSession";

            LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                   EDebugLevelManaged.DebugInfo, "Function Entered");

            bool bConnected = false;
            int  nTrytimes  = 0;

            while (!bConnected && nTrytimes++ <= MAX_TIMES)
            {
                DatabaseSession currentSession = m_sessionStore.GetCurrentSession();
                if (currentSession != null && currentSession.IsTransactionOpen())
                {
                    LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                           EDebugLevelManaged.DebugInfo, "Transaction open in current Session");

                    if (currentSession.CheckDatabaseConnection())
                    {
                        LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                               EDebugLevelManaged.DebugInfo, "Function Exited");
                        return(currentSession);
                    }
                }
                else
                {
                    string connectionString = DBConnectionStrings.GetInstance().GetConnectionString();
                    if (connectionString != null)
                    {
                        DatabaseSession session = m_sessionStore.GetSession(connectionString);
                        if (session == null)
                        {
                            CreateDatabaseSession(connectionString);
                            session = m_sessionStore.GetSession(connectionString);
                        }
                        if (session.CheckDatabaseConnection())
                        {
                            /*if (m_DBSwitched.Length != 0 && (m_DBSwitched == connectionString))
                             * {
                             *  m_DBSwitched = "";
                             * }*/
                            m_sessionStore.StoreCurrentSession(session);
                            LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                                   EDebugLevelManaged.DebugInfo, "Function Exited");
                            return(session);
                        }
                        else
                        {
                            // m_DBSwitched = connectionString;
                            // DBConnectionStrings.GetInstance().UpdateDBStatus(connectionString, DBStatus.DB_OFFLINE);
                        }
                    }
                }
            }
            throw new DatabaseConnectionException("No Database Connection");
        }
Esempio n. 3
0
 public bool IsDatabaseConnectable()
 {
     try
     {
         DatabaseSession session = m_sessionFactory.GetDatabaseSession();
         return(session.CheckDatabaseConnection());
     }
     catch (DatabaseConnectionException e)
     {
     }
     return(false);
 }
Esempio n. 4
0
        public void CreateDatabaseSession(string ConnectionString)
        {
            string FUNCTION_NAME = "CreateDatabaseSession";

            LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                   EDebugLevelManaged.DebugInfo, "Function_Entered");

            DatabaseSession session = new DatabaseSession(ConnectionString);

            m_sessionStore.Store(ConnectionString, session);

            LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                   EDebugLevelManaged.DebugInfo, "Function_Exited");
        }
Esempio n. 5
0
        public bool IsDatabaseConnectable(string ConnectionString)
        {
            DatabaseSession session = m_sessionFactory.GetDatabaseSession(ConnectionString);

            return(session.CheckDatabaseConnection());
        }
Esempio n. 6
0
        //public void BeginTransaction()
        //{
        //    string FUNCTION_NAME = "BeginTransaction";
        //    LogHelper.Trace(CLASS_NAME, FUNCTION_NAME, "Function_Entered");
        //    DatabaseSession session = m_sessionFactory.GetDatabaseSession();
        //    try
        //    {
        //        if (session != null)
        //        {
        //            session.OpenConnection();
        //            session.BeginTransaction();
        //        }
        //    }
        //    catch (Exception localException)
        //    {
        //        LogHelper.Error(CLASS_NAME, FUNCTION_NAME, localException.ToString());
        //        throw localException;
        //    }
        //    LogHelper.Trace(CLASS_NAME, FUNCTION_NAME, "Function_Exited");
        //}

        //public void CommitTransaction()
        //{
        //    string FUNCTION_NAME = "CommitTransaction";
        //    LogHelper.Trace(CLASS_NAME, FUNCTION_NAME, "Function_Entered");
        //    DatabaseSession session = m_sessionFactory.GetDatabaseSession();
        //    try
        //    {
        //        if (session != null)
        //        {
        //            session.CommitTransaction();
        //        }
        //    }
        //    catch (Exception localException)
        //    {
        //        LogHelper.Error(CLASS_NAME, FUNCTION_NAME, localException.ToString());
        //        throw localException;
        //    }
        //    LogHelper.Trace(CLASS_NAME, FUNCTION_NAME, "Function_Exited");
        //}


        //public void RollbackTransaction()
        //{
        //    string FUNCTION_NAME = "RollbackTransaction";
        //    LogHelper.Trace(CLASS_NAME, FUNCTION_NAME, "Function_Entered");
        //    DatabaseSession session = m_sessionFactory.GetDatabaseSession();
        //    try
        //    {
        //        if (session != null)
        //        {
        //            session.RollBackTransaction();
        //        }
        //    }
        //    catch (Exception localException)
        //    {
        //        LogHelper.Error(CLASS_NAME, FUNCTION_NAME, localException.ToString());
        //        throw localException;
        //    }
        //    LogHelper.Trace(CLASS_NAME, FUNCTION_NAME, "Function_Exited");
        //}



        //public string GetStringFromQuery(string cmdQuery)
        //{
        //    string FUNCTION_NAME = "GetStringFromQuery";
        //    LogHelper.Trace(CLASS_NAME, FUNCTION_NAME, "Function_Entered");
        //    LogHelper.Debug(CLASS_NAME, FUNCTION_NAME, string.Format("Query = {0}", cmdQuery));

        //    DatabaseSession session = m_sessionFactory.GetDatabaseSession();
        //    bool bLocalSession = false;
        //    string tempString = "";
        //    try
        //    {
        //        if (session != null)
        //        {
        //            if (!session.IsConnectionOpen())
        //            {
        //                session.OpenConnection();
        //                bLocalSession = true;
        //            }
        //            IDbCommand cmd = session.CreateCommand();
        //            cmd.CommandText = cmdQuery;
        //            IDataReader drOracle = null;
        //            drOracle = cmd.ExecuteReader();
        //            while (drOracle.Read())
        //            {
        //                if (!drOracle.IsDBNull(0))
        //                    tempString += drOracle.GetString(0) + ",";
        //            }
        //        }
        //    }
        //    catch (Exception localException)
        //    {
        //        LogHelper.Error(CLASS_NAME, FUNCTION_NAME, localException.ToString());
        //    }
        //    finally
        //    {
        //        if (bLocalSession)
        //        {
        //            session.CloseConnection();
        //        }
        //    }

        //    if (tempString.Trim() != "" && tempString.LastIndexOf(",") + 1 == tempString.Length)
        //    {
        //        tempString = tempString.Substring(0, tempString.Length - 1);
        //    }
        //    LogHelper.Trace(CLASS_NAME, FUNCTION_NAME, string.Format("Function_Exited with value {0}", tempString));
        //    return tempString;
        //}

        //public IDataReader ExecuteQuery(string query)
        //{
        //    string FUNCTION_NAME = "ExecuteQuery";
        //    LogHelper.Trace(CLASS_NAME, FUNCTION_NAME, "Function_Entered");
        //    LogHelper.Debug(CLASS_NAME, FUNCTION_NAME, string.Format("Query = {0}", query));
        //    DatabaseSession session = m_sessionFactory.GetDatabaseSession();
        //    bool bLocalSession = false;
        //    IDataReader dr = null;
        //    if (session != null)
        //    {
        //        try
        //        {
        //            if (!session.IsConnectionOpen())
        //            {
        //                session.OpenConnection();
        //                bLocalSession = true;
        //            }

        //            IDbCommand cmd = session.CreateCommand();
        //            cmd.CommandText = query;
        //            dr = cmd.ExecuteReader();

        //            LogHelper.Trace(CLASS_NAME, FUNCTION_NAME, "Function_Exited");
        //             return dr;
        //        }
        //        catch (Exception localException)
        //        {
        //            LogHelper.Error(CLASS_NAME, FUNCTION_NAME, localException.ToString());
        //            todo
        //             throw exception
        //        }
        //    }
        //    /*if (bLocalSession)
        //    {
        //        session.CloseConnection();
        //    }*/
        //    LogHelper.Trace(CLASS_NAME, FUNCTION_NAME, "Function_Exited");
        //    return dr;
        //}

        public IDataReader ExecuteQueryWithParams(string query, List <SqlParameter> parameters)
        {
            string FUNCTION_NAME = "ExecuteQueryWithParams";

            LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                   EDebugLevelManaged.DebugInfo, "Function Entered");

            LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                   EDebugLevelManaged.DebugInfo, string.Format("Query = {0}", query));


            DatabaseSession session       = m_sessionFactory.GetDatabaseSession();
            bool            bLocalSession = false;
            IDataReader     dr            = null;

            try
            {
                if (session != null)
                {
                    if (!session.IsConnectionOpen())
                    {
                        session.OpenConnection();
                        bLocalSession = true;
                    }
                    query = session.ReplaceParamelimiterString(query);
                    IDbCommand cmd = session.CreateCommand();
                    cmd.CommandText = query;
                    foreach (var item in parameters)
                    {
                        IDbDataParameter parameter = cmd.CreateParameter();
                        parameter.ParameterName = item.ParameterName;
                        parameter.DbType        = item.DbType;
                        parameter.Direction     = item.Direction;
                        parameter.Value         = item.Value;
                        cmd.Parameters.Add(parameter);
                        //cmd.Parameters.Add(item);
                    }
                    dr = cmd.ExecuteReader();


                    LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                           EDebugLevelManaged.DebugInfo, "Function Exited");
                    //return dr;
                }
            }
            catch (Exception localException)
            {
                LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                       EDebugLevelManaged.DebugInfo, localException.ToString());
            }


            /*if (bLocalSession)
             * {
             *  session.CloseConnection();
             * }*/

            LogHelperCli.GetInstance().Log_Generic(CLASS_NAME + "." + FUNCTION_NAME, LogHelperCli.GetInstance().GetLineNumber(),
                                                   EDebugLevelManaged.DebugInfo, "Function Exited");
            return(dr);
        }