C# - Know location of database file dynamically

Asked

Viewed 143 times

1

I am making a program using C# in WPF, and I need to access the database dynamically, in this case I am using a .mdf.file. In code I have connectionString like this:

string stringConexao = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\PAP\Trabalhos\Trabalhos\Trabalhos.mdf; Integrated Security=True";

I want to replace "E: PAP Jobs Jobs Jobs.mdf" with a dynamic way to find the database, so I can then use the program outside my computer.

The first " Jobs " corresponds to where is the "Jobs.sln", the other folder "Jobs" and "Packages". In the following folder is where you have all . Cs, . xaml, etc.

One of the solutions I found was to put the database inside the /Debug, however I believe it is not the appropriate way to do this.

From now on, thank you.

  • bully. send the paths. of the files I asked for. the complete path so that I know le forward to the best way

3 answers

1


REFERENCE TO THE APP FOLDER. E:\PAP\aplicativo.exe

'|DataDirectory|\' is the folder in which the 'aplicativo.exe' is.

CASE 1: '\aplicativo.exe' and '\Trabalhos.mdf' are in the same folder
THAT IS TO SAY: 'E:\PAP\aplicativo.exe' and 'E:\PAP\Trabalhos.mdf'

AttachDbFilename=|DataDirectory|\Trabalhos.mdf;

CASE 2: '\aplicativo.exe' and '\Trabalhos.mdf' are in different folders
THAT IS TO SAY: 'E:\PAP\aplicativo.exe' and 'E:\PAP\Trabalhos\Trabalhos\Trabalhos.mdf'

AttachDbFilename=|DataDirectory|\Trabalhos\Trabalhos\Trabalhos.mdf;

SUMMING UP: Use this code. this code. that works.

string stringConexao = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Trabalhos\Trabalhos\Trabalhos.mdf; Integrated Security=True";
  • If I put as you said, you will search inside the bin folder Debug Jobs Jobs Jobs.mdf, which in this case does not exist. And I have the database before Debug bin. I don’t know if it is the proper location but inside the Debug bin whenever I turn on the Program the data is erased

  • Perhaps the most appropriate way to ask this question would be. Where the database should be located?

  • Files in separate partitions?

  • As I had the database, inside Debug, it erased every time I started the program again, however I have already been told what I had to do for it not to delete, ie now when using AttachDbFilename=|DataDirectory|\Trabalhos.mdf; already solves the problem. Thank you and sorry for the confusion! :)

  • I’ve been through it. rs rs rs worst I’ve been through it in rs rs production environment

0

Hello, the answer is simple.

Make sure that this database is always present in the root directory of the project executable. So, you can use the line of code that I will show below to get the location of the project folder: Application.StartupPath;

I hope I’ve helped!!

  • I know that Application.StartupPath works in Winforms, however I am using WPF, in which such a command does not exist.

  • Sorry, I have no experience with WPF, I thought that by being part of . Net Framework also had this command available.

0

I searched and found 2 ways to find the root directory of a WPF application.

You can do it like this:

System.AppDomain.CurrentDomain.BaseDirectory

Or so:

System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)

I hope this time I’ve helped! :)

  • Yes, now checking that produces an identical result to |DataDirectory|. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.