Search code examples
c#sql-server-2008connectiondatabase-server

Connecting to a database server


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-


Solution

  • 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();
             }