locked
Connection strings in the data access layer RRS feed

  • Question

  • If I am creating a data access layer and this assembly needs to be run independantly of other libraries (business layer, UI) then how can I get a connection string? Should I create a app.config file for the data access layer assembly and put the connectionstring inside the app.config for the dal.dll? Do I need to rename the app.config file to the name of my assebly.config?

    I tried but couldn't get it find the string (object not set to an instance of an object error), maybe I have something incorrectly configured. When I put it into a web.config file in a web site it detects it.

    Here is what I put in the Membership.DataAccessLayer.Sql.config file and what I used to access it.

     

    this

     

    .connectionString = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;

    and here is my .config file

    <?

     

     

    xml version="1.0" encoding="utf-8"

    ?>

    <

     

     

    configuration

    >

    <

     

     

    connectionStrings

    >

    <

     

     

    add name="DB" connectionString="Data Source=somesource; Initial Catalog=somecatalog; User ID=someid; Password=something"

    />

    </

     

     

    connectionStrings

    >

    </

     

     

    configuration

    >

     

     


    chuckdawit
    Wednesday, December 7, 2011 8:36 PM

Answers

  •  

    Put an entry in web.config this way:

    <add name="ConnectionString" connectionString="SERVER=168.10.5.6;uid=sa;pwd=pass@123;database=abc"/>
    


    And in C# code read it this way:

     if (ConfigurationManager.ConnectionStrings["ConnectionString"] == null)
                    {
                        throw new Exception("Connection String not specified");
                    }
                    return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    


    I suppose in web.config instead of providing datasource you need to provide database.

    I hope it helps.


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful"
    • Marked as answer by witdaj Wednesday, December 21, 2011 8:08 PM
    Thursday, December 8, 2011 4:30 AM

All replies

  • Hi

    I guess you should build ConfigurationManager by specifying path..you will find one of the static methods available for this purpose.

    Hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    Thursday, December 8, 2011 3:46 AM
  •  

    Put an entry in web.config this way:

    <add name="ConnectionString" connectionString="SERVER=168.10.5.6;uid=sa;pwd=pass@123;database=abc"/>
    


    And in C# code read it this way:

     if (ConfigurationManager.ConnectionStrings["ConnectionString"] == null)
                    {
                        throw new Exception("Connection String not specified");
                    }
                    return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    


    I suppose in web.config instead of providing datasource you need to provide database.

    I hope it helps.


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful"
    • Marked as answer by witdaj Wednesday, December 21, 2011 8:08 PM
    Thursday, December 8, 2011 4:30 AM
  • The config files are assigned per application domain. And There is no built-in support for DLL or assembly based configuration file in .Net.

    So you need to mention all the configuration setting of your assembly in your main Caller Application's Configuration i.e why mentioning in web.config works fine.

    There is some work around of it and below links should help you to do that.
    http://stackoverflow.com/questions/594298/c-sharp-dll-config-file
    http://www.bearcanyon.com/dotnet/#AssemblySettings

     


    Lingaraj Mishra
    Thursday, December 8, 2011 12:35 PM