.NET,包括带有 Windows 应用程序部署的类库数据库(和其他资源)

发布于 2024-09-26 06:37:47 字数 328 浏览 1 评论 0原文

我有一个类库,它附加了一个驻留在其数据目录中的小型 SQL Server 数据库。当我将这个类库与另一个 Windows 应用程序一起使用时,我发现一旦编译代码,数据库文件就会复制到我的 Windows 应用程序项目的 bin 文件夹中。但是,当我发布 Windows 应用程序、安装并运行它时,出现错误“尝试附加文件 C:\Documents and Settings\User\Local Settings\Apps\2.0\Data..\ 的自动命名数据库” DB.mdf 失败。显然这个文件夹没有mdf文件。

我想如果我只是将数据库文件添加到我的 Windows 应用程序项目中,这不会成为问题。但肯定有更好的方法吗?

I have a class library that attaches itself a tiny SQL Server database that resides in its Data Directory. When i'm using this class library with another windows application i see that once i compile my code, the database files get copied to the bin folder of my windows app project. However when i publish the windows app,install, and run it, i get the error 'An attempt to attach an auto-named database for file C:\Documents and Settings\User\Local Settings\Apps\2.0\Data..\DB.mdf failed.' Obviously this folder doesn't have the mdf files.

I guess this won't be a problem if i just add the database files to my windows application project. But surely there's a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-10-03 06:37:47

您可以将用于创建数据库的 SQL 脚本包含到您的“安装/首次运行例程”中。

我想您已经说过,安装 SQL Server 是一个先决条件。

对于数据文件,我建议您使用变量连接字符串来访问数据库。这样,您可以更改安装例程,包括询问用户希望将数据文件安装在哪里,并将其作为连接字符串的一部分保存到 app.config 文件中。

相反,您也可以使用用户选择的应用程序安装位置来覆盖代码中为数据库存储的相对路径(使用与上面提到的相同的连接字符串变量)。

You could include an SQL script for creating the database into your "installation/run first time routine".

I guess that you've already stated that having a form of SQL Server is an installtion prerequisite.

For the data files I would recommend that you use a variable connection string for accessing your database. That way you can change the installation routine to include asking the user where they wish to have the data files installed and save that as part of your connection string to the app.config file.

Conversely you could also use the users selection of where to install the app to override the relative path stored for the database within your code (using the same connection string variable as mentioned above).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文