Exemplo n.º 1
0
        public static SBDatabase getDbh(bool new_connection = false)
        {
            if (SBFactory.dbh_config == null)
            {
                throw new Exception("Database connection data is not assigned");
            }
            SBDatabase _dbh = null;

            if (SBFactory.FactoryType == "web")
            {
                if (HttpContext.Current.Items["dbh"] != null)
                {
                    return((SBDatabase)HttpContext.Current.Items["dbh"]);
                }
                Console.WriteLine("Creation database instance");
                //create a new instance of database handler
                if (SBFactory.dbh_config["db_type"].ToString().Equals("sql_server"))
                {
                    _dbh = new SqlServer(SBFactory.dbh_config["con_string"].ToString());
                    _dbh.open();
                }
                if (SBFactory.dbh_config["db_type"].ToString().Equals("mysql"))
                {
                    _dbh = new MySQL(SBFactory.dbh_config["con_string"].ToString());
                    if (!_dbh.open())
                    {
                        throw new Exception("Enable to connect MySql Database");
                    }
                }
                if (SBFactory.dbh_config["db_type"].ToString().Equals("sqlite"))
                {
                    _dbh = new SQLite(SBFactory.dbh_config["con_string"].ToString());
                    if (!_dbh.open())
                    {
                        throw new Exception("Unable to connect to SQLite Database");
                    }
                }
                HttpContext.Current.Items["dbh"] = _dbh;
                return(_dbh);
            }
            else
            {
                if (SBFactory.dbh == null || new_connection)
                {
                    Console.WriteLine("Creation database instance");
                    //create a new instance of database handler
                    if (SBFactory.dbh_config["db_type"].ToString().Equals("sql_server"))
                    {
                        _dbh = new SqlServer(SBFactory.dbh_config["con_string"].ToString());
                        _dbh.open();
                    }
                    if (SBFactory.dbh_config["db_type"].ToString().Equals("mysql"))
                    {
                        _dbh = new MySQL(SBFactory.dbh_config["con_string"].ToString());
                        if (!_dbh.open())
                        {
                            throw new Exception("Enable to connect MySql Database");
                        }
                    }
                    if (SBFactory.dbh_config["db_type"].ToString().Equals("sqlite"))
                    {
                        _dbh = new SQLite(SBFactory.dbh_config["con_string"].ToString());
                        if (!_dbh.open())
                        {
                            throw new Exception("Unable to connect to SQLite Database");
                        }
                    }
                    if (new_connection)
                    {
                        return(_dbh);
                    }
                    SBFactory.dbh = _dbh;
                }
            }

            return(SBFactory.dbh);
        }
Exemplo n.º 2
0
 public static void DestroyDbh()
 {
     SBFactory.dbh = null;
 }