Using an SQL Database in Multiple Projects

I have a system consisting of 5 applications. Each application accesses the database through the DAL library. In DAL, I have app.config with the following entry:

<connectionStrings>
<add name="DataAccessLayer.Properties.Settings.ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\something\something\MyDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
</connectionStrings>

Using the full path for attachDbFilename works fine. But I am not satisfied:

  • I copied the app.config file to every application using the database. The best way to do this is to copy the DAL app.config as a link in other projects?

  • I don't want the full path when it comes to deployments that won't work. Relative paths in app.config do not work. Ideally, I would like to be able to pull the DAL from the source control to any computer and not worry about changing the connection string every time. This: http://blogs.msdn.com/b/smartclientdata/archive/2005/08/26/456886.aspx talks about | DataDirectory | for deployment, but this does not work for me when debugging (if I'm not mistaken, see 3)

  • This may be better as a separate issue, but this is related to 2. - Is there a “good” way to organize multiple projects for debugging? I created Bin dir, and in each project setup I copy dll / exe to this bin directory. I also have a copy of the database here (I have not tried the path in app.config, but that also did not work, and DataDirectory |). It is also incredibly annoying that relative paths do not work in Debug \ Working Directory settings, so it looks like this is one place that will need to be changed every time the code is checked on a new machine?

Sorry for war and peace and in advance in advance for any ideas.

+3
source share
2 answers

- :

1) app.config , . - DAL app.config ?

, :

<connectionStrings configSource="connectionStrings.config" />

:

<connectionStrings>
   <add name="DataAccessLayer.Properties.Settings.ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\something\something\MyDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" providerName="System.Data.SqlClient" />
</connectionStrings>

, app.config, .

2) , , . app.config .

, , , - placeholder |DataDirectory|, App_Data ASP.NET.

   <add name="DataAccessLayer.Properties.Settings.ConnectionString" 
        connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|MyDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True" 
        providerName="System.Data.SqlClient" />

- - , .

+4

AttachDbFilename , MDF, . . User Instance=True , , , - , , .

MDF SQL , : Data Source=.\SQLEXPRESS;Initial Catalog=<dbname>; Integrated Security=True.

+4

Source: https://habr.com/ru/post/1758640/


All Articles