Code sample for getting all Sql Database names in the sql instance in .Net

By the help of following code you can get all Sql Database of any sql instance Data table Object so that you can easily display these Database names to user. In the following code we are binding all database in drop down control.

First you need to add following references-


    Private Sub BindDatabaseNames(ByVal SqlAuth As Boolean, ByVal cmbDataBase1 As ComboBox)
        Dim serverName As String = "Put your server name"
        Dim serverConnection As Microsoft.SqlServer.Management.Common.ServerConnection = New Microsoft.SqlServer.Management.Common.ServerConnection()
        serverConnection.ServerInstance = serverName
        serverConnection.LoginSecure = True
        If SqlAuth Then
            serverConnection.LoginSecure = False
            serverConnection.Login = "Put Login name"
            serverConnection.Password = "Put password"
        End If
        Dim server As Server = New Server(serverConnection)
            For Each db As Database In server.Databases
        Catch ex As Exception
            Dim exception As String = ex.Message
        End Try
    End Sub


private void BindDatabaseNames(bool SqlAuth, ComboBox cmbDataBase1)
	string serverName = "Put your server name";
	Microsoft.SqlServer.Management.Common.ServerConnection serverConnection = new Microsoft.SqlServer.Management.Common.ServerConnection();
	serverConnection.ServerInstance = serverName;
	serverConnection.LoginSecure = true;
	if (SqlAuth) {
		serverConnection.LoginSecure = false;
		serverConnection.Login = "Put Login name";
		serverConnection.Password = "Put password";
	Server server = new Server(serverConnection);
	try {
		foreach (Database db in server.Databases) {
	} catch (Exception ex) {
		string exception = ex.Message;

Author: Ankur

Have worked primarily in the domain of Calling, CRM and direct advertisers services. My technological forte is Microsoft Technologies especially Dot Net (Visual Studio 2003, 2005, 2008, 2010 and 2012) and Microsoft SQL Server 2000,2005 and 2008 R2. My Area of Expertise is in C#. Net, VB.Net, MS-SQL Server, ASP. Net, Silverlight, HTML, XML, Crystal Report, Active Reports, Infragistics, Component Art, ComponeOne, Lead Tools etc.