Chuck Norris
Chuck Norris

Reputation: 15200

How to get connection string from another project in LINQ to SQL?

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?

Upvotes: 2

Views: 4085

Answers (1)

gideon
gideon

Reputation: 19465

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

Upvotes: 4

Related Questions