简体   繁体   English

如何使用Visual Studio在C#应用程序中发布Access数据库?

[英]How do I publish an Access database with my C# application using Visual Studio?

I'm trying to figure out how to publish an application I wrote. 我试图弄清楚如何发布我编写的应用程序。 We didn't get to publishing in class, and my programming teacher is having some health issues and isn't available right now. 我们当时没上课,我的程序设计老师遇到了一些健康问题,目前无法使用。

I found the "Publish" option, and can get it to create a folder with an install program... but I open it, and it just opens the program, and spams me infinitely, complaining that my Access database (located in the bin > debug in the build stage) can't be accessed (from some weird path I don't recognize). 我找到了“发布”选项,可以用安装程序来创建一个文件夹...但是我打开了它,它只是打开了程序,并无限地向我发送垃圾邮件,抱怨我的Access数据库(位于bin中) >在构建阶段进行调试)(从我不认识的一些奇怪的路径)无法访问。 I tried using WiX, but it gave me an error when I tried to install, saying it doesn't have access to the install folder (running as admin). 我尝试使用WiX,但尝试安装时却出现错误,说它无权访问安装文件夹(以admin身份运行)。 I've been googling for a few hours, poking at it, exploring, and I'm not getting too far. 我已经搜查了几个小时,戳了一下,探索了一下,而且距离还不算太远。 Can anyone ELI5? 任何人都可以ELI5吗?

When you use the "publish" option for desktop apps, VS creates a click-once installer that will place all the files it knows about in the appropriate locations. 当您将“发布”选项用于桌面应用程序时,VS将创建一个单击一次安装程序,该安装程序会将其了解的所有文件放置在适当的位置。

Unfortunately, it can't guess which other files your application needs so you need to tell it explicitly. 不幸的是,它无法猜测您的应用程序需要哪些其他文件,因此您需要明确地告诉它。

If you right-click the Project->Properties, go to the Publish tab and click the "Application Files" button, you'll see all files that will be added to the installer. 如果右键单击“项目”->“属性”,请转到“发布”选项卡,然后单击“应用程序文件”按钮,您将看到将添加到安装程序中的所有文件。

Next, click "Show all files" at the bottom. 接下来,单击底部的“显示所有文件”。 Find your database, and change the Publish Status to "Data File". 查找数据库,然后将“发布状态”更改为“数据文件”。

Note that I've only ever used the Click Once installer to install static files (like images/documentation) that are never modified, only replaced in later releases. 请注意,我只使用过一次单击安装程序来安装永不修改的静态文件(如图像/文档),仅在以后的发行版中将其替换。 I'm not sure whether your (modified) db will be preserved during an update but I suspect not. 我不确定在更新过程中是否会保留您的(修改过的)数据库,但我怀疑不会。

If the Click Once install process is too simple for your needs, VS2010 has "Setup Projects" which create more complex installers that support logic/code. 如果“单击一次”安装过程过于简单,无法满足您的需要,则VS2010会提供“安装项目”,以创建支持逻辑/代码的更复杂的安装程序。 For VS2012, the commonly suggested option is Wix . 对于VS2012,通常建议的选项是Wix Unfortunately, it's got a steep learning curve but it can do pretty much anything you need. 不幸的是,它的学习曲线很陡峭,但是它几乎可以完成您需要的任何事情。

I believe VS2013 and later have setup projects again through an extension but I haven't tried it myself. 我相信VS2013及更高版本通过扩展再次设置了项目,但我自己没有尝试过。

Edit: 编辑:

The easiest way around this is likely to set the connection string programatically based on where the application is executing from. 解决此问题的最简单方法可能是根据应用程序的执行位置以编程方式设置连接字符串。

Note that as per this answer clickonce apps are usually executed from deep inside the user profile directory (also read the answer below about data directories). 请注意,按照此答案, clickonce应用程序通常是在用户配置文件目录的深处执行的(另请阅读以下有关数据目录的答案 )。 It's a side-effect of how ClickOnce works (it wants to install somewhere the user is guaranteed to have write access). 这是ClickOnce工作方式的副作用(它希望安装在保证用户具有写访问权的位置)。

Check if there really is an .mdb in that folder. 检查该文件夹中是否确实存在.mdb。 If not, you need to tweak the installer or the properties for the .mdb . 如果不是,则需要调整.mdb的安装程序或属性。 Assuming it's in the same location as the executable, you can tell your application where to find it... 假设它与可执行文件位于同一位置,则可以告诉应用程序在哪里找到它...

string dbPath = IO.Path.Combine(
           AppDomain.CurrentDomain.BaseDirectory,
           "access.mdb");

string connectionString = String.Format(
           "Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}",
           dbPath);

I'm not sure why you think the database file added by the installer is in a directory directly under C:\\ . 我不确定您为什么认为安装程序添加的数据库文件位于C:\\下的目录中。 It's possible you're not looking at the file created by the installer. 您可能没有查看安装程序创建的文件。

To avoid confusion, try changing the name from access.mdb to something else ( temp.mdb ?), build the installer, rename back to access.mdb . 为避免混淆,请尝试将名称从access.mdb更改为其他名称( temp.mdb ?),构建安装程序,然后重命名为access.mdb Now, when you've installed the application, make sure the file you're looking at is now called temp.mdb . 现在,在安装应用程序后,请确保您正在查找的文件现在称为temp.mdb If not, you're looking at the wrong file. 如果不是,则说明您查找的文件错误。

This Link Has Full demonstration of Database Connectivity And Publish a C# application with database. 此链接具有数据库连接的完整演示,并带有数据库发布C#应用程序。 The application is also running on another machines. 该应用程序还在另一台计算机上运行。 How to Publish C# Application with access database 如何使用Access数据库发布C#应用程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#应用程序中嵌入字体? (使用Visual Studio 2005) - How do I Embed a font with my C# application? (using Visual Studio 2005) 如何使用Access数据库使我的C#应用​​程序在LAN上使用一个数据库运行 - how do i make my C# application using Access Database to run on LAN using one database 如何使用访问数据库发布C#应用程序 - how to publish C# application with the access database (Visual Studio) 如何将 powershell 脚本导入 c# 项目,以便我可以将 .NET 应用程序与脚本一起发布 - (Visual Studio) How to import powershell scripts into c# project so that I can publish my .NET application together with the scripts 如何从Visual Studio中开发的Android C#应用程序访问SoftLayer对象存储。 - How do I access SoftLayer object storage from a C# application for Android, developed in Visual Studio. 如何在 Mac 上的 Visual Studio 中编译 C# 应用程序? - How do I compile a C# application on a Mac in Visual Studio? 如何在Visual Studio中使用C#访问Microsoft Excel 365 - How do I access Microsoft Excel 365 using C# in Visual Studio 如何使用OleDb将Visual Studio c#中MS-Access的值插入到textBox中? - how do I insert into a textBox a value from MS-Access in visual studio c# using OleDb? 每次我运行Visual Studio C#应用程序时,Access 2007中的所有记录都会删除 - all records in access 2007 deletes each time i run my visual studio c# application 使用c#将Access数据库添加到Visual Studio 2010中的项目中 - Adding Access Database to Project in Visual Studio 2010 using c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM