1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

How to get a list of databases in an SQL Server (programatically)

Discussion in 'Software Development' started by wango, Jan 25, 2002.

Thread Status:
Not open for further replies.
  1. wango

    wango Thread Starter

    Nov 13, 2001
    How do I programatically get a list of all databases on a known server?
    What I need is to make my code as flexible as possible. I suceeded in (1) listing all the availble SQL Servers in the domain; (2) I was able to create a DSN in ODBC dynamically but wit a hardcoded database as in

    RETCODE retcode;
    LPTSTR szDriver = "SQL Server";

    char szAttributesChar[255];

    // add datasource name if it exixts
    dsn_str = "DSN=" + m_MyProgramaticDSN + "\0";
    LPTSTR lpszMyProgramaticDSN = new TCHAR[dsn_str.GetLength()+1];
    _tcscpy(lpszMyProgramaticDSN, dsn_str);

    server_str = "SERVER=" + m_cmbSQLservername + "\0";
    LPTSTR lpszSQLservername = new TCHAR[server_str.GetLength()+1];
    _tcscpy(lpszSQLservername, server_str);

    LPTSTR lpszDESCRIPTION = "DESCRIPTION=Dynamically created datasetname\0";


    // remove the local datasource name if it exixts
    TCHAR szAttributesRemove[1028];
    TCHAR* szSubStringRemove = &(szAttributesRemove[0]);
    // remove an existing DSN
    BOOL bretcode = SQLConfigDataSource(NULL, ODBC_REMOVE_SYS_DSN, szDriver,
    &(szAttributesRemove[0]) /*szAttributes*/);

    As you can see, the database is hardcoded. If I can figureout a way of programtically getting it, I will be okay and that is what I am looking for.

    Note that I have not yet connected to the database.
  2. elsinaj


    Aug 14, 1999
    I guess you already know this, but the SQL command for this is "show databases". I don't know if this helps anything?

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/66389

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice