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.
CodePudding user response:
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).