如何在 vb.net 中创建设置? ?举例说明
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=C:\solutionvs10\test\test\bin\Debug\db2.mdb;**Jet OLEDB:Database Password=secret**")
cn.Open()
''# codes
cn.Close()
我用的是VS 2010 上面是我的代码......你可以看到它是Ms Access DataBase......(密码保护) 我如何创建一个设置......可以轻松安装其他计算机......
我可以创建这样的设置吗? ?
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=C:\solutionvs10\test\test\bin\Debug\db2.mdb;**Jet OLEDB:Database Password=secret**")
cn.Open()
''# codes
cn.Close()
I used V S 2010
The above is my code......u can see that it is Ms Access DataBase....(password protection)
How i can create a setup .....that can be easy to install other computer.........
Can i Create such a setup ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,您发布的这段代码是错误的:
错误的原因是您无法保证关闭数据库连接,最终导致应用程序无法访问数据库的状态。这在您的开发测试中不会发生,因为此类测试往往是短暂的,但它会发生在您的用户身上,他们倾向于让应用程序运行更长的时间。
现在,我知道您在想,“是的,我正在关闭我的连接。您没有看到
cn.Close()
吗?”是的,我看到了。但这还不够好。有几种情况(例外是最重要的)可能导致此代码无法运行。编写这段代码的正确方法是这样的:VB.Net 中有一个简写语法,如下所示:
现在,讨论实际的部署问题。
您需要将安装项目添加到保存应用程序项目的同一解决方案中。然后,您应该能够使用主项目的输出作为安装项目的包,并将初始数据库文件作为内容资源包含在内。安装项目中不需要包含任何其他内容; Windows 包含开箱即用的 Access 使用的 Jet 数据库引擎。您可能需要花一些时间确保适当的 .Net 框架运行时将作为依赖项安装(如果需要)。
First of all, this code you posted is wrong:
The reason it's wrong is because you're not guaranteed to close your database connections, eventually leading to a state where your database is not accessible to your application. This won't happen in your development testing, because such tests tend to be short-lived, but it will happen to your users, who tend the keep applications running over longer periods of time.
Now, I know you're thinking, "Yes, I am closing my connections. Don't you see the
cn.Close()
?" Yes, I see it. But that's not good enough. There are a few cases (exceptions are the big one) that can cause this code not to run. The correct way to write this code is like this:And there's a short-hand syntax in VB.Net that looks like this:
Now, on to the actual deployment question.
You need to add a setup project to the same solution that holds your application project. You should then be able to use the output of your main project as the package for the setup project and include your initial database file as a content resource. There is no need for anything else included in the setup project; Windows includes the Jet database engine used by access out of the box. You may want to spend some time making sure that the appropriate .Net framework runtime will be installed as a dependency if needed as well.
正如威尔指出的,将 .mdb 文件包含到您的项目中可能就是您正在寻找的。您可以在项目管理器中执行此操作,然后调整属性,以便将文件发布到应用程序文件夹(复制到输出目录 = 始终复制)。
在代码中,您应该引用数据库,
因为您不知道数据库在客户端计算机上的安装路径。
另一方面,您可能需要确保将应用程序发布为 32 位应用程序,因为 Jet4.0 不适用于 64 位应用程序(请参阅 http://connect.microsoft.com/VisualStudio/feedback/details/123311/win-xp-x64-jet-v4 -0)
As Will pointed out including the .mdb file into your project is probably what you are looking for. You can do it in the project manager and then adjusting the properties so that the file is published to the application folder (Copy to Output Directory = Copy Always).
In the code, you should then refer to the database as
since you don't know the installation path of your database on the client's computer.
On the other side you might want to make sure that you publish your application as a 32bits application since the Jet4.0 does not work for 64bits applications (see http://connect.microsoft.com/VisualStudio/feedback/details/123311/win-xp-x64-jet-v4-0)
为了在另一台计算机上轻松安装,我推荐 SQLite 或 SQL Server Compact。访问配置也可能比 sqlite 或 mssql Compact 更困难。
除此之外,我建议使用 ClickOnce 部署应用程序。 此处的文档。
For easy install on another computer, I recommend SQLite or SQL Server Compact. Configuration may also be more difficult with access than sqlite or mssql compact.
Beyond that, I would recommend deploying the application using ClickOnce. Documentation here.
我认为将 MDB 文件作为项目的一部分,并设置正确的属性以将其复制到应用程序文件夹中就可以解决问题。
另一方面,您必须确保设置
MDAC 2.8
作为您的项目依赖项之一,以便通知或要求您的用户将其下载并安装到他们的计算机上。我确实认为您可以通过
ClickOnce
进行部署,否则创建一个SetUp 项目
,这是可行的。I think that making the MDB file part of your project, and setting the right property for it to be copied into the application folder will do the trick.
On the other hand, you will have to make sure you set the
MDAC 2.8
as one of your project dependencies so that your users are either informed or required to download and install it to their computers.I do think you may deploy through
ClickOnce
, otherwise create aSetUp project
, that is doable.