none
How to get connectionstrings RRS feed

  • Question

  • I'm trying to get a connectionstring from my config file.

     

    After adding a reference to the System.Configuration namespace to my code I tried to  run this code which I copied from here: http://msdn2.microsoft.com/en-us/library/system.configuration.connectionstringsettings(vs.80).aspx

     

    tatic void ShowConnectionStrings()
    {
         // Get the application configuration file.
        System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);
    
        // Get the conectionStrings section.
        ConnectionStringsSection csSection =
            config.ConnectionStrings;
        
        for (int i = 0; i < 
            ConfigurationManager.ConnectionStrings.Count; i++)
        {
            ConnectionStringSettings cs = 
                csSection.ConnectionStringsIdea;
            
            Console.WriteLine("  Connection String: \"{0}\"",
                cs.ConnectionString);
    
            Console.WriteLine("#{0}", i);
            Console.WriteLine("  Name: {0}", cs.Name);
         
            
            Console.WriteLine("  Provider Name: {0}", 
                cs.ProviderName);
            
        }
    
    }
    The above code is not something I wrote, it is copied from here: http://msdn2.microsoft.com/en-us/library/system.configuration.connectionstringsettings(vs.80).aspx
     
     
    Wednesday, August 22, 2007 5:59 AM

All replies

  • You forgot to ask your question? At first sight it seems nothing is wrong with your code...


            static void Main( string[] args )
            {
                for(int i = 0; i < ConfigurationManager.ConnectionStrings.Count; ++i)
                {
                    ConnectionStringSettings settings = ConfigurationManager.ConnectionStringsIdea;
                    Console.WriteLine(settings.ConnectionString);
                }

                Console.ReadKey();
            }
    Wednesday, August 22, 2007 10:06 AM
  •  

    The code does not compile.  Is it missing a reference to a namespace??  The link I referenced says namespace is system.configuration, which I used but no luck.  Your code does not compile either.

    Also I am using Visual C# 2005 Express in case that makes any difference.

     

    Thanks for the reply.

    Wednesday, August 22, 2007 2:35 PM
  • Moving your mouse over the 'unknown type' and hitting shift+alt+f10 would have solved the error (as it generates the required using statements).

    Under the assumtpion you've add a reference to System.Configuration the following code should compile (and work)

    Code Snippet

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Configuration;

    namespace Config
    {
        class Program
        {
            static void Main(string[] args)
            {
                for (int i = 0; i < ConfigurationManager.ConnectionStrings.Count; ++i)
                {
                    ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings[i];
                    Console.WriteLine(settings.ConnectionString);
                }

                Console.Write("{0}Press any key to continue...", Environment.NewLine);
                Console.ReadKey();
            }
        }
    }




    Wednesday, August 22, 2007 2:44 PM
  • Sorry but it does not work.  The mouse/shift+alt+f10 thing does nothing.  That is a helpful tip I hope I can make that work.  Here is the complete text of my program file with error msgs below:

    Thanks for your help.

     

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using System.Data.SqlClient;

    using System.Configuration;

     

    public class TestClass

    {

    static void ShowConnectionStrings()

    {

    // Get the application configuration file.

    System.Configuration.Configuration config =

    ConfigurationManager.OpenExeConfiguration(

    ConfigurationUserLevel.None);

    // Get the conectionStrings section.

    ConnectionStringsSection csSection =

    config.ConnectionStrings;

    for (int i = 0; i <

    ConfigurationManager.ConnectionStrings.Count; i++)

    {

    ConnectionStringSettings cs =

    csSection.ConnectionStringsIdea;

    Console.WriteLine(" Connection String: \"{0}\"",

    cs.ConnectionString);

    Console.WriteLine("#{0}", i);

    Console.WriteLine(" Name: {0}", cs.Name);

     

    Console.WriteLine(" Provider Name: {0}",

    cs.ProviderName);

    }

    }

    static void ConnStringz(string[] args)

    {

    for (int i = 0; i < ConfigurationManager.ConnectionStrings.Count; ++i)

    {

    ConnectionStringSettings settings = ConfigurationManager.ConnectionStringsIdea;

    Console.WriteLine(settings.ConnectionString);

    }

    Console.Write("{0}Press any key to continue...", Environment.NewLine);

    Console.ReadKey();

    }

    }

     

     

    Error 1 The type or namespace name 'Configuration' does not exist in the namespace 'System.Configuration' (are you missing an assembly reference?) 

    Error 2 The name 'ConfigurationManager' does not exist in the current context 
    Error 3 The name 'ConfigurationUserLevel' does not exist in the current context 
    Error 4 The type or namespace name 'ConnectionStringsSection' could not be found (are you missing a using directive or an assembly reference?) 

    Error 5 The name 'ConfigurationManager' does not exist in the current context 
    Error 6 The type or namespace name 'ConnectionStringSettings' could not be found (are you missing a using directive or an assembly reference?) 

    Error 7 The name 'ConfigurationManager' does not exist in the current context 

    Error 8 The type or namespace name 'ConnectionStringSettings' could not be found (are you missing a using directive or an assembly reference?) 

    Error 9 The name 'ConfigurationManager' does not exist in the current context 

    Wednesday, August 22, 2007 3:30 PM
  • Afaik this means, unlike what you said, that you haven't added a reference to System.Configuration !


    (Project -> Add Reference -> .NET -> Choose System.Configuration and click on OK)


    Wednesday, August 22, 2007 3:32 PM
  •  

    Fixed, thank you.

    I do not understand why I have I have to add a reference the way you described.  I added a using statement, shouldnt that do the same thing??

    Wednesday, August 22, 2007 3:55 PM
  • - The import statement tells the compiler that you will 'shorter' names.. eg: with a using System.Configuration you can write ConfigurationManager  and the compiler will know that you're talking about System.Configuration.ConfigurationManager...

    - Adding a reference tells the compiler that it should into the referenced assembly for data (eg: the implementation of a given class).

    Wednesday, August 22, 2007 4:04 PM