How to create Sql Synonym or “Alias” for Database Name?

后端 未结 4 1509
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 10:00

I\'m using ms sql 2008 and trying to create a database name that references another database. For example \'Dev\', \'Test\', \'Demo\' would be database names that i could re

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 10:52

    I've done something similar to this using another config file.

    The new config file maps your generic name to all of the information needed to connect to that database (db name, user name, password, etc.) and then your connection function takes your generic name as an argument.

    db.config:

    DEV_DB_NAME = db20080101
    DEV_DB_USER = dev_user
    DEV_DB_PASS = dev_pass
    TEST_DB_NAME = db20070101
    TEST_DB_USER = test_user
    TEST_DB_PASS = test_pass
    

    connection code:

    db_connection get_connection(string prefix) {
        db_connection db_conn = new db_connection;
        string db_name = get_config_value(config_path, prefix + "_DB_NAME");
        string db_user = get_config_value(config_path, prefix + "_DB_USER");
        string db_pass = get_config_value(config_path, prefix + "_DB_PASS");
    
        db_conn.connect(db_name, db_user, db_pass);
    
        return db_conn;
    }
    

    Then you just call get_connection() with your db alias as the argument.

提交回复
热议问题