0

There is a database server at IP address 192.168.1.11. There are several databases on that server. It has authentication, like user : System and pass : 123123 .

Now I want to connect to this server only, not any particular database, and then get a list of databases available on that server.

I know the normal procedure of connecting to a database with SqlConnection. But I'm wondering how I could just get connected to the server and get the list of the databases on that server.

I am using Visual Studio 2010 and SQL Server 2008-

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Abdur Rahim
  • 3,975
  • 14
  • 44
  • 83
  • do you need to do this in code? in c#? Your question is, how do i list all the databases that I have access to on a MSSQL2008 server using C#? – nycynik Dec 03 '12 at 15:36
  • actually, i have to first select a server name(as i am working in a network). after i select a server name, databases in that server should be listed. – Abdur Rahim Dec 03 '12 at 15:39
  • 1
    You cannot connect *just to the server* - you are **always** connecting to a database on that server. You could however connect to that server and the `master` database and then run the query that Obama has suggested – marc_s Dec 03 '12 at 15:45
  • ok. I got my answare. Thanks for the information @marc_s – Abdur Rahim Dec 03 '12 at 15:48

1 Answers1

2

run this query on a Method

SELECT [name] 
FROM master.dbo.sysdatabases 
WHERE dbid > 4 

or by

String connString ="Data Source=localhost;User ID=username;Password=passwrd;";

        using (SqlConnection sqlConn = new SqlConnection(connString))
        {
            sqlConn.Open();
            DataTable tblDatabases = sqlConn.GetSchema("Databases");
            sqlConn.Close();
            DataTable td = tblDatabases.Select("dbid>6").CopyToDataTable();
         }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Obama
  • 2,586
  • 2
  • 30
  • 49
  • 1 question. I am not understanding why we are using dbid>4? is it constant? is it like that, 4 databases are common in a server? – Abdur Rahim Dec 03 '12 at 15:53
  • System databases are under <4 so we set >4 to not include system databases on your list :) – Obama Dec 03 '12 at 15:58