I'm using C# with framework 4.0 and SQL server 2008 R2. I have listed the SQL server 2008 with this code:
public static string[] GetSQLServerList()
{
SqlDataSourceEnumerator dse = SqlDataSourceEnumerator.Instance;
DataTable dt = dse.GetDataSources();
if (dt.Rows.Count == 0)
{
return null;
}
string[] SQLServers = new string[dt.Rows.Count];
int f = -1;
foreach (DataRow r in dt.Rows)
{
string SQLServer = r["ServerName"].ToString();
string Instance = r["InstanceName"].ToString();
if (Instance != null && !string.IsNullOrEmpty(Instance))
{
SQLServer += "\\" + Instance;
}
SQLServers[System.Math.Max(System.Threading.Interlocked.Increment(ref f), f - 1)] = SQLServer;
}
Array.Sort(SQLServers);
return SQLServers;
}
i listed my Server on ComboBox.
How can I list the database, depends on which server that I choose in ComboBox?
I found this tutorial, but it needs sqlconnection
, how can I connect when I have not chosen the server ?
This is how you get a list of server names on the network:
To Get a List of databases from selected server: