2

I've 3 different projects in one solution. I placed my connection string in first project like this

 <connectionString name="My Connection String">
     <parameters>
     <parameter name="Integrated Security" value="True" />
     <parameter name="server" value=".\SQLEXPRESS" isSensitive="true" />
     <parameter name="database" value="MyDatabase" isSensitive="false" />
     </parameters>
  </connectionString>

Now in another project I make LINQ to SQL class and it generated app.config file and make it's connection string in it? How can I make to read connection string from my exisiting .config file from another project?

4
  • you want to get the connection string of one project from another project? You just have to expose it via some class/property Commented Feb 29, 2012 at 6:49
  • Can you explain it? I want to change my LINQ to SQL class's connection string to read it from another project's connection string. Commented Feb 29, 2012 at 6:51
  • is that connectionString setted in a web.config or an app.config file inside your solution? Commented Feb 29, 2012 at 6:54
  • It's in project's web.config file. Commented Feb 29, 2012 at 6:55

1 Answer 1

4

I'm assuming you're using Application Settings and have a connection string in your FirstProject.

Like this:

enter image description here

The generated Settings class is marked internal sealed partial .. so you can't directly access it via MyProject.Properties.Settings...

You just create a class to expose it:

namespace FirstProject
{
    public class ThisProjectSettings
    {
        public static string ConnectionString
        {
            get
            {
                return Settings.Default.Conn;
            }
        }
    }
}

Then use it from your second project like this:

FirstProject.ThisProjectSettings.ConnectionString
Sign up to request clarification or add additional context in comments.

2 Comments

I've thought about this. But in this case, I don't want to add first project's reference to second. I don't think it correct, because first is Backend, and second is UI. They might be independent. So is any better way, to make it more generic. For example, make connection string file for all projects.
So the UI is not dependant on the backend in any way? You could roll out your own configuration storage mechanism but you should figure out if you really need to do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.