Get list of all Databases in an SQL server

Hey all, I have a pretty simple question, how do i get a list of all databases on a given SQL server

I just want a list of them, how is this done I know that you can use SMO, but I am acutally trying to do this without using SMO. Can it be done

Thanks to anyone who helps


Answer this question

Get list of all Databases in an SQL server

  • yeukwong999

    thanks for the help, I was looking to do it through code, rather than through sql, but this certainly makes another task I was going to do easier, thanks!

  • shreeman

    How should the list look like if not as a ’table list
  • ltan68219

    Yeah I did look into this but doesnt this only give table lists I would like a list of the databases within a Server.

  • pbergeron

    the code I have used to get what I wanted was as follows:

    SqlConnection sqlConn = new SqlConnection("Server=theServer; Database=dbName; User Id=user; password=password");

    sqlConn.Open();
    DataTable tblDatabases = sqlConn.GetSchema("Databases");
    sqlConn.Close();

    foreach (DataRow row in tblDatabases.Rows)
    {
    databasesList.Items.Add(row["database_name"]);
    }


    Seemed to work just fine.

    Thanks to all who helped me so far

  • Don Griest

    Hi, there are many options, some of them are listed below:

    --SQL2k5

    Select name from sys.sysdatabases

    --Pre SQL2k5

    Select name from dbo.sysdatabases

    --System procedure

    sp_helpdb

    HTH, jens Suessmeyer.


  • Grigori Somin

    Hi,

    You could use GetOledbSchemaTable method of the OledbConnection class. It allows to get schema information about objects in a database. Remember that you will need appropriate permissions on a server to query this data. Check next link with the sample

    http://support.microsoft.com/kb/309488/en-us



  • Get list of all Databases in an SQL server