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;