0

I´ve been having problems with my project and the database it has. I have created a simple project and I created an access database in it, the database is in the project folder. This is the connection string specified in the App.config:

 <connectionStrings>
        <add name="Project.My.MySettings.BaseDeDatosConnectionString"
            connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Recursos\Datos\BaseDeDatos.accdb"
            providerName="System.Data.OleDb" />
  </connectionStrings>

But when I publish my project, install it in my computer and executed it I get the error:

'C:\Users\USER_NAME\AppData\Local\Apps\2.0\Data\6QPXHXGQ.ZM0\MVT91HXD.V1B\simu..tion_0000000000000000_0001.0000_49edcdec2714f7aa\Data\Resources\Data\Database.accdb' is not a valid path . Make sure that the path is spelled correctly and that you are connected to the server where the file is located.

And of course the program remains useless. I´ve checked the folder and indeed there is no data base in there, I didn't even knew it would create a file there (I know nothing related to databases and deployment, I am just following tutorials). I was using a SQL data base before and it worked fine after installation, but I had to change it. What could I be missing?

I am using VS 2015 in spanish and VB.NET.

Bryan
  • 48
  • 3
  • Could you perhaps update your question with the exact details of how you're creating the deployment and how you're including the DB as requirement for deployment (if at all). Is the DB ending up anywhere on the machine after deployment. And just be aware C:\Users\USER_NAME would mean it's only being deployed and available to a single user. Is that intended? – Hursey Jun 14 '22 at 21:59
  • [Where is |DataDirectory| defined?](https://stackoverflow.com/q/12187068/7444103) - [ADO.NET |DataDirectory| where is this documented?](https://stackoverflow.com/q/1409358/7444103) -- Specify what kind of Project you have built and the .Net version you're targeting. Add the corresponding Tags. -- When the app is first run, you could copy/move or create the database in `Environment.SpecialFolder.CommonApplicationData` under a directory that may represent the name of your app. If it's a WinForms app, you can use `Application.CommonAppDataPath`. Set `|DataDirectory|` to that path as described. – Jimi Jun 14 '22 at 22:51
  • Is the database visible in the Solution Explorer for your project? Is it under the 'Recursos\Datos' folder? If you select that item and open the Properties window, what are the `Build Action` and `Copy to Output Directory` properties set to? How EXACTLY are you deploying your application? – John Jun 15 '22 at 01:21
  • @Hursey - Yes, it is intended to be used just for one user, the one in the computer. I am deploying in the most simple way:, I click "built" in VS, I click in "publish", it creates a setup file and then I install it using that file. I don´t set up anything else. I don´t know if that answer your question. – Bryan Jun 15 '22 at 15:55
  • @John - Yes, the database is visible in the solution explorer. Built action is set up as: "Content". Copy to Output Directory is set up as "Copy always" – Bryan Jun 15 '22 at 15:58

1 Answers1

0

If you are using the Publish functionality in VS then you are using ClickOnce. ClickOnce apps have a dedicated folder to which "|DataDirectory|" resolves at run time. It is NOT the program folder. If you want to use "|DataDirectory|" in your connection string then you have to specify that the data file is indeed a data file via the properties, so that it will be placed in that folder. I don't really use ClickOnce but I believe that, to do that, you need to open the Publish page of the project properties, click the Application Files button and then set the Publish Status of that file to Data File. I'm not sure whether the subfolders for the source file will be honoured after publishing or not but you can test that.

Note that, if you simply copy the application files to the target machine or use some installer technology other than ClickOnce then "|DataDirectory|" will resolve to the program folder. For the record, it resolves to the App_Data folder for ASP.NET apps (possibly only Web Forms, not sure).

John
  • 3,057
  • 1
  • 4
  • 10
  • That was the solution @John. Thank you for your knowledge, I will also take into consideration what you have said the DataDirectory. – Bryan Jun 16 '22 at 21:43