使用数据库打包C#应用程序 [英] Packaging a C# application with a database

查看:125
本文介绍了使用数据库打包C#应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经打包了使用数据库的WPF C#应用程序。



但是,当我安装打包的应用程序时,它使用我创建的数据库,视觉工作室(.mdf),而不是创建另一个。



包装应用程序的整个目的是,我可以共享它在其他计算机,我需要它为应用程序的实例创建一个新的数据库。



有办法做到这一点吗?
有没有属性我需要设置说数据库是本地实例?



任何帮助都将非常感激。



感谢您的支持。 。

解决方案

刚刚自己做的这件事,我建议使用SQL Compact Edition使过程成为一个很容易,但如果它的进程太迟了,那么坚持使用当前数据库,但我不知道以下方法是否与一个MDF文件工作。



如何在客户端计算机上部署应用程序?用户计算机是否安装了sql?



您需要做的第一件事是转到MDF的属性,并确保复制到输出目录设置为复制如果更新并将构建操作设置为内容。



如果您使用安装项目来安装应用程序,那么您需要确保在项目,应用程序属性,发布,应用程序



并右键单击安装程序文件中的应用程序文件夹系统,然后添加,
项目输出,文件内容



最后更改代码中的连接字符串,使其读为

 使用(SqlCeConnection sqlCon = new SqlCeConnection(@Data Source = | DataDirectory | \App.sdf; Persist Security Info = False;)

或如果您未使用精简版

  using(SqlConnection sqlCon = new SqlConnection(@Data Source = | DataDirectory | \App.mdf; Persist Security Info = False;)
pre>

^使用DataDirectory意味着您不在乎用户放置应用程序的位置,您将始终获得正确的文件



之前运行和更改您的数据库压缩,它不支持很多东西一个正确的sql数据库。



可能仍然会遇到权限错误,并且因为它很长的时间,我可能错过了一步,所以只是评论,如果你需要更多。



编辑
如果用户没有sql server或sql Compact,你最简单的解决方案是要求他们安装SQL CE,或者有一种方法使用DLL做同样的事情,但我不能得到它工作



此处的链接为 http://msdn.microsoft.com/en-us/library/aa983326.aspx


I have packed a WPF C# application that is using a database.

However, when I install the packaged application it is using the database I created at the start through visual studio (.mdf) rather than creating another one.

The whole purpose of packaging the application was so that I could share it on other computers, so I was install it I need it to create a new database for that instance of the application.

Is there a way to accomplish this? Is there a property I need to set to say that the database is a local instance? Or force the package to create a new one on installation?

Any help would be much appreciated.

Thanks.

解决方案

Just been in the middle of doing this myself, one thing I would suggest is using SQL Compact Edition makes the process a lot easier, but if it's too late in the process at the moment then stick with the current database, However I do not know if the below method works with an MDF file.

How are you deploying the application on the client machine? does the users machine have sql installed?

The first thing you need to do is go to the properties of your MDF and ensure copy to output directory is set to copy if newer and build action is set to content.

If you're using a setup project to install the application then you need to make sure in project, application properties, publish, application files that you're mdf is in the list and is set to Data File auto, required and include.

and right click on the "Application Folder" in the installers file system, then add, project output, file contents

Finally change the connection string in your code so that it reads

using ( SqlCeConnection sqlCon = new SqlCeConnection( @"Data Source=|DataDirectory|\App.sdf;Persist Security Info=False;" )

or if you're not using compact Edition

using ( SqlConnection sqlCon = new SqlConnection( @"Data Source=|DataDirectory|\App.mdf;Persist Security Info=False;" )

^using "DataDirectory" means that you don't care where the user puts the application, you will always get the right file

before you run and change your database to compact, it doesnt support a lot of things a proper sql database will.

After all this, you may still encounter errors with permissions and because its quite long winded I might have missed out a step so just comment if you need more?

EDIT If the user does not have either sql server or sql Compact, your easiest solution would be to require them to install SQL CE or there is a way of using DLLs that do the same thing but I couldn't get that to work

the link for that is here http://msdn.microsoft.com/en-us/library/aa983326.aspx

这篇关于使用数据库打包C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆