英文:
generate an executable in vb.net with access database
问题
我在vb.net中创建了一个库存管理应用程序,使用了一个Access数据库。
现在我想使用VS 2022部署它,但在部署后安装时,数据库不见了。
我已将数据库文件添加到解决方案中,并在Visual Studio中运行时成功运行,但部署后看不到数据库文件。
我该如何解决这个问题?
英文:
I created a stock management application in vb.net with an access database.
Now I want to deploy it with VS 2022, but when I install it after deployment the database is not present.
I added the database file to the solution and it works successfully when running in Visual Studio, but I can’t see the database file after I deploy it.
How can I fix this?
答案1
得分: 1
首先,Visual Studio Solution Explorer 为解决方案中的每个文件提供了一个属性部分。非代码文件有一个属性,用于在构建期间如何处理文件,其中包括“不复制”、“如果较新则复制”和“始终复制”等选项。您需要选择后两个选项之一。
其次,您提到安装程序,这通常意味着将文件添加到“Program Files”文件夹中。默认情况下,自从从FAT32过渡到NTFS以来,标准用户就没有写入此文件夹的权限,这至少可以追溯到2003年,对大多数人来说可能更久。这意味着即使您成功复制文件,您的用户也无法对数据库文件进行更改。
为了解决这个问题,您仍然可以选择将数据库部署到“Program Files”文件夹,这是最简单的选项,但是只将其视为参考数据库文件。然后,您需要将代码添加到应用程序的启动过程中,以将此部署的数据库复制到特定的“应用程序数据”文件夹,以一种不会覆盖现有文件的方式进行复制。在这种情况下,请不要调用 File.Exists()
,而是_始终_调用 File.Copy()
不覆盖现有文件的重载。
英文:
A couple things to keep in mind:
First, Visual Studio Solution Explorer provides a Properties section for each file in the solution. Non-code files have a property for how to treat the file during build, which includes options to "Do not copy", "Copy if Newer", and "Always Copy". You want one of latter two.
Second, you mention installing the program, which often means adding the files to the Program Files folder. By default, standard users have not had write access to this folder since transitioning from FAT32 to NTFS, which goes back to at least 2003 for most people, if not older. This means your users will not be able to make changes to the database file, even if you get it to copy correctly.
To resolve this, you can still just deploy the DB to the Program Files folder if you want, which is the easiest option, but then only treat this as a reference DB file. You then add code to your application's startup process that copies this deployed DB to the special Application Data folder in such a way that it will not overwrite the existing file if it already exists. Do this without calling File.Exists()
, and instead always call the File.Copy()
overload that will not overwrite an existing file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论