C#win表单应用程序不能在另一台机器上运行 [英] C# win form application does not run on another machine

查看:81
本文介绍了C#win表单应用程序不能在另一台机器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi



i在Visual Studio 2017中有一个用c#编写的win表单应用程序。



这是一个简单的客户端注册表单,其中Linq to SQL使用存储在\bin\debug文件夹中的.mdf数据库文件。



while在我自己的机器上运行应用程序没有问题,它运行正常。但是当我使用VS的发布命令为其创建设置并将其安装在另一台计算机上(例如安装在此笔记本电脑上的虚拟机)时,它会显示一条错误消息,表明它无法连接到数据库。



这是错误信息

>错误消息



我尝试过:



hi

i have a win form application written c# in Visual Studio 2017.

it's a simple client registration form with Linq to SQL that uses a .mdf database file stored in the \bin\debug folder.

while running the application on my own machine there is no problem, and it runs fine. but when i create a setup for it using publish command of the VS and installing it on another machine ( for instance the virtual machine installed on this laptop), it shows an error message that it could not connect to the database.

this is the error message
error message

What I have tried:

connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\SQLDB2mdf.mdf;Integrated Security=True;Connect Timeout=30"

推荐答案

这有几个原因。
首先,只有SQL Server的Express版本支持附加数据库:it;一种特殊的调试模式,可以旋转一个新的独立SQL服务器实例来支持它。你永远不应该尝试将它用于生产,只能用于开发。

第二个是你的代码要求文件在数据目录中,你的解释说它在可执行文件夹中。

第三个是如果你的数据库在bin / debug文件夹中,那么SQL服务器无法访问它,因为它不能在你的用户ID下运行。

第四个是如果它在可执行文件夹中,它不能写入生产中,因为它将在Write Files限制的Program Files文件夹下,所以当SQL Server尝试打开它进行读取/写访问它将获得访问冲突。

第五个是你的连接字符串要求SQL服务器实例是本地的:即在与客户端相同的PC上。这意味着客户端不仅必须在每台PC上安装SQL Server,而且还不能在机器之间共享登录详细信息:每台PC都有自己的登录集。



还有其他的东西,比如你不应该对连接字符串进行硬编码,因为它往往导致上面的问题一到五,但这是你的选择,而不是我的选择。
There are several reasons for this.
The first is that only Express editions of SQL Server support Attached Databases: it;s a special "debugging" mode that spins up a new, separate instance of SQL server to support it. You should never try to use it for production, only ever development.
The second is that your code requires the file to be in the data directory, and your explanation says it's in the executable folder.
The third is that if your DB is in the bin/debug folder, it won't be accessible to SQL server anyway, as it doesn't run under your user ID.
The fourth is the if it is in the executable folder, it can't be written to in production, as this will be under the Program Files folder which is write restricted, so when SQL Server tries to open it for read/write access it will get an access violation.
The fifth is that your connections string requires the SQL server instance to be local: i.e. on the same PC as the client. Which means that not only does the client have to have SQL Server installed on each PC, but that the login details cannot be shared between machines: each PC will have it's own set of logins.

There are other things, like you shouldn't hard code connection strings because it tends to cause problems one through five above, but that's your choice, not mine.


这篇关于C#win表单应用程序不能在另一台机器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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