-3

I am working on a project and it is a Windows based .NET application. I want to make a relative path so that when I move my files to another computer, I won't set up a new connection again.

So far, here's what I have done.

In my app.config:

<connectionStrings> 
    <add name="ConString"
         connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\dbOffense.mdf;Integrated Security=True;Connect Timeout=30"
         providerName="System.Data.SqlClient" /> 
</connectionStrings>

In my program. The code is not in any function. It is globally declared.

Dim constr As String=ConfigurationManager.ConnectionStrings("ConString").ConnectionString()
Dim con As New SqlConnection(constr)

My database dbOffense.mdf is located in the same folder as my Windows application OffenseDatabase. It is not in the folder where the .exe file of my application is.

So far, all that I've read doesn't really work at all.

I have tried moving my dbOffense.mdf to bin/Debug/Database directory. Then whenever I remove and set up a new connection/data source in my .NET application, the dataset is generated in the same folder with my Windows application OffenseDatabase instead of the same folder where dbOffense.mdf is.

Please can someone help?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NoobCoder
  • 73
  • 1
  • 9
  • When you say "the same folder as my Windows application OffenseDatabase", are you talking about the project folder? – jmcilhinney May 18 '17 at 06:49
  • @jmcilhinney yes. I'm still looking for solutions to my problem, if I were to share my current situation. – NoobCoder May 18 '17 at 08:19

1 Answers1

1

You actually don't have a problem but you're trying to create one. Local data files just work as they are but some people don't realise and make their lives more difficult. Here's how it works.

You add the data file to your project and it gets saved in the project folder. Like all the other files in the project folder, it is a SOURCE file. It does not get used at run time. If you want to make any changes to your database schema or default data, you do so in that source file. If you generate a typed DataSet or Entity Framework model or the like then you do it from that file.

When you build your project, the files that get used at run time are placed in the output folder. For a Debug build, that is generally 'bin\Debug' under the project folder and for a Release build it is generally 'bin\Release'. Your source data file is copied and placed in that output folder along with the EXE and any other output files. It is that copy that you connect to at run time, NOT the source file. One obvious advantage of this is that, when it comes time to deploy, your Release output will contain a nice clean copy of your source file rather then your having to clean up a file that you were using for debugging.

By default, the Copy To Output Directory property of the data file is set to Copy Always. What that means is that, every time you make a change to your code and build again, the old copy in the 'bin\Debug' folder will be overwritten. This is not what a lot of people want. Many, if not most, people want their test data to persist between builds. In that case, you simply set that property to Copy If Newer and then a new copy will only be made when you make changes to the source file.

So, in short, your connection will work as it is unless you have done something to break the normal workings of things. The "|DataDirectory|" place-holder wil resolve to the correct folder at run time. Just make sure that your source file has been added to the project and the only thing that you may need to do is change the Copy To Output Directory property.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • @jmcillhinney Um hi. I'm back again. Your suggestion worked but when the database is edited/updated it reverts back to the original data. Here's the scenario: from my machine, the one of the tables in the database has five inputs, then when I passed it on to the other computer, it reverted back to only having one data. It only comes out of the blue without me knowing the reason. – NoobCoder Jun 12 '17 at 11:11
  • the only ones that I changed to `Copy If Newer` were my .mdf file and my .xsd file. – NoobCoder Jun 12 '17 at 11:16
  • `Copy If Newer` means that the source file will be copied over the working file any time you build and the last modification time of the source file is after the last modification time of the working file. That will generally only occur if you change the schema or data in the source file, in which case it is essential that the working file is over-written. The IDE might set the last modification time of the source file any time you open it though. That's just something that you have to live with. If it is a problem, take a copy of your working file yourself and restore it as required. – jmcilhinney Jun 13 '17 at 14:00
  • I see. You are right. Every time, in the Server Explorer, I open the modify my .mdf files, it reverts back to the source copy's data. At first, I thought it was a problem with my coding while in fact it was the feature of the `Copy If Newer` function. Thank you, @jmcillhinney – NoobCoder Jun 13 '17 at 14:18