Click here to Skip to main content
15,921,697 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
C#
using System.Data.Sql;
using System;

class Program
{
    static void Main()
    {
        // Retrieve the enumerator instance and then the data.
        SqlDataSourceEnumerator instance =
          SqlDataSourceEnumerator.Instance;
        System.Data.DataTable table = instance.GetDataSources();

        // Display the contents of the table.
        DisplayData(table);

        Console.WriteLine("Press any key to continue.");
        Console.ReadKey();
    }

    private static void DisplayData(System.Data.DataTable table)
    {
        Console.WriteLine("Inside DisplayData");
        foreach (System.Data.DataRow row in table.Rows)
        {
            foreach (System.Data.DataColumn col in table.Columns)
            {
                Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
            }
            Console.WriteLine("============================");
        }
    }
}
Posted
Updated 13-Feb-12 2:16am
v2
Comments
[no name] 13-Feb-12 8:17am    
What do you mean "any output"? None of the WriteLine methods are working? I find that difficult to believe. More than likely you don't have any rows in the DataTable.
nishantcomp2512 13-Feb-12 8:21am    
is this program display Press any key to continue.
then might having your data is blank.
Herman<T>.Instance 13-Feb-12 8:24am    
I see no try..catch construction so even System.Data.DataTable table = instance.GetDataSources(); can lead to an exception and no output is shown because program is terminated.
Michael dg 13-Feb-12 8:24am    
it seems the code is perfectly fine. I think you have to check if your data source or data table has a data.
Herman<T>.Instance 13-Feb-12 8:25am    
shouldn't
<pre lang="c#">SqlDataSourceEnumerator instance =
SqlDataSourceEnumerator.Instance;</pre>
be
<pre lang="c#">SqlDataSourceEnumerator instance =
SqlDataSourceEnumerator.Instance();</pre>?

Your code is correct, have you started SQL server?
Can you connect to any instance via SQL server management studio? Does you code work afterwards?
The only reason I can see that that software would not show more than "Press any key to continue." is if no SQL instances hgave been found.
 
Share this answer
 
Comments
Herman<T>.Instance 13-Feb-12 8:41am    
the code is straight forward copied from MSDN: http://msdn.microsoft.com/en-us/library/system.data.sql.sqldatasourceenumerator.getdatasources
OriginalGriff 13-Feb-12 9:43am    
Yes - I said it was correct, I checked it against what I use.
Since it works for me, check your instances.
Since this piece of code worked quite well in my environment there could be a couple of reasons:

  1. Impatience: Just wait a minute or maybe even two.
  2. Discovery doesn't work due to network constraints.
  3. There are no SQL Servers to discover.


Regards,

Manfred
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 13-Feb-12 11:23am    
Best answer so far. My 5.
--SA
Below code works for me


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Sql;

namespace CP_329450_this_Csharp_program_is_not_showing_any_output_why
{
    class Program
    {
        static void Main()
        {
            // Retrieve the enumerator instance and then the data.
            SqlDataSourceEnumerator instance =
              SqlDataSourceEnumerator.Instance;
            System.Data.DataTable table = instance.GetDataSources();

            // Display the contents of the table.
            DisplayData(table);

            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
        }

        private static void DisplayData(System.Data.DataTable table)
        {
            foreach (System.Data.DataRow row in table.Rows)
            {
                foreach (System.Data.DataColumn col in table.Columns)
                {
                    Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
                }
                Console.WriteLine("============================");
            }
        }
    }
}


But will useally take time to browse all sql server instances.

You can try :
1) start import / export wizard
2) try to drop down source connection combo box.
3) and see how much time it will take. The same time above code will take.

If you debug you will know it will halt at

C#
System.Data.DataTable table = instance.GetDataSources();

this line.


Hope this helps if yes then accept and vote the answer
--Rahul D.
 
Share this answer
 

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