Click here to Skip to main content
15,898,968 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to create a test program. In this program i want to list all my local sql instances including the sql server name. Windows form app or windows console app. Please help or give me some advice.

What I have tried:

C#
static void Main(string[] args)
        {
            using (System.Data.DataTable sqlSources = 
            SqlDataSourceEnumerator.Instance.GetDataSources())
            {
                foreach (System.Data.DataRow source in sqlSources.Rows)
                {
                    string servername;
                    string version = source["Version"].ToString();
                    string instanceName = source["InstanceName"].ToString();
                    if (!string.IsNullOrEmpty(instanceName))
                     {
                         servername = string.Concat(source["InstanceName"], "\\", 
                         source["ServerName"]);
                     }
                     else
                     {
                         servername = source["ServerName"].ToString();
                         version = source["Version"].ToString();
                         instance = source["InstanceName"].ToString();
                     }
                    Console.WriteLine(" Server Name:{0}", servername);
                    Console.WriteLine("Version:{0}", source[version]);
                    Console.WriteLine("Instance:{0}", source[instanceName]);
                    Console.ReadLine();
                }
Posted
Updated 11-Dec-17 23:26pm
Comments
CHill60 12-Dec-17 5:14am    
What is wrong with the code you have?
Member 13475897 12-Dec-17 5:57am    
it is returning the server name but not the instances in the server
phil.o 12-Dec-17 5:25am    
Is it possible you have reversed the server and instance names?
It is rather ServerName\\InstanceName imho.
Member 13475897 12-Dec-17 5:57am    
Im trying to list the instances in my server, the server name is returning correctly. The version returns as a null and the instance is returning a blank. The version isn't important but i'd like to list all the instances in my server

1 solution

Names must match:
C#
string instanceName = source["InstanceName"].ToString();
...
instance = source["InstanceName"].ToString();
Try
C#
string instanceName = source["InstanceName"].ToString();
...
instanceName = source["InstanceName"].ToString();
Then, use your collected data in your WriteLines:
C#
Console.WriteLine("Server Name:{0}", servername);
Console.WriteLine("Version:{0}",version);
Console.WriteLine("Instance:{0}", instanceName);


Quote:
How would i go about in going futher and displaying the databases in the server's instances


C#
using (SqlConnection con = new SqlConnection(connectionStringToAServerInstance))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("sp_databases", con))
        {
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataReader read = cmd.ExecuteReader();
        while (read.Read())
            {
            Console.WriteLine((string)read["DATABASE_NAME"]);
            }
        }
    }
 
Share this answer
 
v2
Comments
Member 13475897 12-Dec-17 6:04am    
The instance name is returning a blank when i put break points in the Console.WriteLine("Instance:{0}", instanceName); part. Want i mean by instance name is i want to list the instances in my local sql server. Currently there is only 1 instance
OriginalGriff 12-Dec-17 6:28am    
Well, that makes some sense - if the instance name is blank after this:
string instanceName = source["InstanceName"].ToString();
Then this test fails:
if (!string.IsNullOrEmpty(instanceName))
So you do this again in the else part:
instanceName = source["InstanceName"].ToString();

So you need to look at the rest of "source" and see what you are getting - when I run it on my system I get my server instances. What do you get for ServerName and version?
Member 13475897 12-Dec-17 6:38am    
My correct local sql server name and the version is also correct but i saw some websites say that if you use the default instance it will return blank , some comment on this please. ServerName:DESKTOP-FEQ6FKG and version:13.0.1601.5
OriginalGriff 12-Dec-17 7:06am    
I've not seen that - my local default dev instance returns:
Server Name:SQLEXPRESS\GRIFF-DESKTOP
Version: 11.0.5058.0
Instance:SQLEXPRESS
(I'm stuck with SQL Server 2012 to match the production side server)
Member 13475897 12-Dec-17 7:35am    
thanks helped alot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900