Gareth
Gareth

Reputation: 1

How to retrieve a list of databases on a specified server in C#?

I have two combo boxes in my form app. When the form loads I do this:

combobox1.items.add(system.environment.machinename).

Thats works. Now i want to populate the second combo box with the databases of the selected server. How do I do that?

private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
   ????????
}

Upvotes: 0

Views: 83

Answers (2)

user1082916
user1082916

Reputation:

If you are using SQL Server, you can have database with following query:

SELECT name
FROM sys.databases

You can execute procedure sp_databases too:

EXEC sp_databases

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176896

Try

in .NET you can use the SQL Server Management Objects

Microsoft.SqlServer.Management.Smo.Server server = new Microsoft.SqlServer.Management.Smo.Server("localhost");
foreach (Database db in server.Databases) {
    Console.WriteLine(db.Name);
}

Upvotes: 1

Related Questions