简体   繁体   English

如何在Visual Studio 2015 / C#中使用本地SQL数据库引用子项目

[英]How to reference sub project with local SQL database in Visual Studio 2015/ C#

I have ac# project ("MainProject") which references another c# project ("UserManager") in my solution. 我的解决方案中有一个ac#项目(“ MainProject”),该项目引用了另一个c#项目(“ UserManager”)。 UserManager has a local database ( a "Service-based Database", .mdf ). UserManager具有本地数据库( a "Service-based Database", .mdf )。

By adding this database to UserManager, Visual Studio 2015 by default created a connectionstring entry in App.config of UserManager... 通过将此数据库添加到UserManager中,Visual Studio 2015默认情况下在UserManager的App.config中创建了一个连接字符串条目...

<connectionStrings>
        <add name="UserManager.Properties.Settings.TPHWDatabaseConnectionString"
            connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\TPHWDatabase.mdf;Integrated Security=True"
            providerName="System.Data.SqlClient" />
</connectionStrings>

... and an entry in Properties.Settings named as the name field of the App.config entry containing the following value: ...,然后在Properties.Settings中的一个条目被命名为App.config条目的名称字段,其中包含以下值:

Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\TPHWDatabase.mdf;Integrated Security=True

In my C# code of the UserManager project I create my SqlConnection object using the following lines: 在UserManager项目的C#代码中,我使用以下几行创建SqlConnection对象:

    // Establish conenction to database
    sqlConnection = new SqlConnection();
    sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["UserManager.Properties.Settings.TPHWDatabaseConnectionString"].ConnectionString;
    sqlConnection.Open();

This all works fine running the UserManager project by itself. 单独运行UserManager项目,一切正常。 The database file (".mdf") is also included in the MainProject's bin folders. 数据库文件(.mdf)也包含在MainProject的bin文件夹中。 When running the MainProject however, "ConfigurationManager.ConnectionStrings ["UserManager.Properties.Settings.TPHWDatabaseConnectionString"]" will be null and I will get a nullpointer exception for calling ".ConnectionString" on it. 但是,在运行MainProject时, "ConfigurationManager.ConnectionStrings [“ UserManager.Properties.Settings.TPHWDatabaseConnectionString”]“将为null并且在调用“ .ConnectionString”时会出现nullpointer异常。 Which seems reasonable since I have no Access to "UserManager.Properties.Settings" and "App.config" from MainProject. 这似乎是合理的,因为我无法从MainProject中访问"UserManager.Properties.Settings""App.config"

So my question is: What is a clean way to reference UserManager ( in MainProject) that will keep the database working? 所以我的问题是:什么是引用UserManager的干净方法(在MainProject中),可以使数据库正常工作?

EDIT 编辑

I now just copied the connection string as is to the App.config file of my MainProject and set the exact same setting to Properties.Setting. 现在,我将连接字符串原样复制到MainProject的App.config文件中,并将完全相同的设置设置为Properties.Setting。 This seems to work and was rather easy. 这似乎很有效,而且很容易。 But is is it still the cleanest way to do so? 但这仍然是最干净的方法吗?

Add a connection string to your web.config referring to the database instead of attaching the DB file. 将引用数据库的连接字符串添加到您的web.config中,而不是附加数据库文件。 something like this 这样的事情

<connectionStrings>
    <add connectionString="Server=.\sqlexpress;Initial Catalog=myDB;Integrated Security=True;" name="mydbconnection" />
 </connectionStrings>

And read it like below: 并像下面这样阅读:

ConnectionStringSettings ConnectionString = ConfigurationManager.ConnectionStrings["mydbconnection"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Visual Studio 2015 中使用 SQL 服务器数据库从 C# 项目创建安装文件 - Create setup file from C# project with SQL server database in Visual Studio 2015 是否可以从C#中的非数据库项目引用Visual Studio SQL项目? - Is it possible to reference a Visual Studio SQL project from a non-database project in C#? 如何在Visual Studio 2015中使用C#创建到SQL Server数据库的连接字符串 - How to create a connection string using C# to SQL Server database in Visual Studio 2015 在Visual Studio Express 2013中为C#项目的本地测试设置SQL数据库的最简单方法 - Easiest way to set up SQL Database for local testing of C# project in Visual Studio Express 2013 在Visual Studio(2015)中添加对C#项目的引用所需的文件类型 - File type required to add a reference to a C# project in Visual Studio (2015) 如何在Visual Studio 2015中使用C#7? - How to use C# 7 with Visual Studio 2015? 使用MS Visual Studio 2015添加本地SQL数据库时出错 - Error to add local sql database with MS Visual Studio 2015 如何在Visual Studio 2010中引用C#类库项目? - How to reference a C# Class Library project in Visual Studio 2010? Visual Studio C#项目参考无法正常工作 - Visual Studio C# Project Reference not working 如何在Visual Studio 2017中从C#代码制作SQL Server 2017本地数据库? - How to make a SQL Server 2017 local database from C# code in Visual Studio 2017?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM