如何从LINQ中的另一个项目中获得连接字符串到SQL? [英] How to get connection string from another project in LINQ to SQL?

查看:121
本文介绍了如何从LINQ中的另一个项目中获得连接字符串到SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个不同的项目,在一个解决方案。我把我的连接字符串中的第一个项目像这样

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>

现在在另一个项目中我做的LINQ to SQL类,它生成的app.config文件,使它的连接字符串中呢?我怎么能做出从其他项目我已有的config文件读取连接字符串?

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?

推荐答案

我假设你正在使用的应用程序设置,并有一个连接字符串您的 FirstProject

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

这样的:

生成的设置类被标记内部密封部分.. ,所以你不能直接通过 MyProject.Properties.Settings .. <访问/ code>。

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;
            }
        }
    }
}

然后使用它从你的第二的项目是这样的:

FirstProject.ThisProjectSettings.ConnectionString

这篇关于如何从LINQ中的另一个项目中获得连接字符串到SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆