[英]Why can't I connect to the SQL Server database when I create setup file in Visual studio?
I create application using C# windows forms which uses local SQL Server database server to store / read data using this connection string: 我使用C#Windows窗体创建应用程序,该窗体使用本地SQL Server数据库服务器使用以下连接字符串存储/读取数据:
SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=mydb;Integrated Security=True");
After that I created a setup file (exe) then I installed the setup file in same PC and the application works well and can read data from the SQL Server database. 之后,我创建了一个安装文件(exe),然后将安装文件安装在同一台PC上,该应用程序运行良好,可以从SQL Server数据库中读取数据。
Now the problem came when I went to another PC I installed SQL Server and I manually attached the same database (mydb) and installed my application and it did not work. 现在,当我转到另一台PC时,出现了问题,我安装了SQL Server,并手动附加了相同的数据库(mydb)并安装了我的应用程序,但它不起作用。 The error is shown in the screenshot:
该错误显示在屏幕截图中:
I have different SQL Server name in the other PC but I specified in the connection string that the SQL Server is Localhost so I don't know why it didn't work. 我在另一台PC上有不同的SQL Server名称,但是我在连接字符串中指定SQL Server为Localhost,所以我不知道为什么它不起作用。 Please help me how to fix this error.
请帮助我如何解决此错误。 Thank you
谢谢
Make sure Sql Server is set to allow Tcp/Ip connections and that it's set to allow integrated security logins. 确保将Sql Server设置为允许Tcp / Ip连接,并且将其设置为允许集成安全性登录。 Then, make sure the user on that machine is granted the correct access to the database.
然后,确保授予该计算机上的用户对数据库的正确访问权限。 You may have better luck using Sql authentication over Windows Authentication here, if you want to be able to distribute this application and have it "just work".
如果您希望能够分发此应用程序并使它“正常运行”,那么在此处使用Sql身份验证而不是Windows身份验证可能会更好。
Finally, if Sql Server here is meant to be a simple datastore for a typical local desktop application, it's probably overkill. 最后,如果此处的Sql Server是要作为典型本地桌面应用程序的简单数据存储,则可能会适得其反。 Full Sql Server (including Express Edition) is a server engine.
Full Sql Server(包括Express Edition)是服务器引擎。 It works best when it's the only thing running on the machine and can use up all of the resources on the machine in order to cache data and handle requests from many remote machines.
当它是机器上唯一运行的东西时,它工作得最好,并且可以用尽机器上的所有资源以缓存数据并处理来自许多远程机器的请求。 If you just want a local data store for a typical desktop application, an in-process engine like Sql Server LocalDb, Sql Server Compact Edition, Sqlite, or even MS Access would be a much more appropriate choice.
如果只想为典型的桌面应用程序提供本地数据存储,则像Sql Server LocalDb,Sql Server Compact Edition,Sqlite甚至MS Access这样的进程内引擎将是更合适的选择。
You need to set up Windows authentication for the user that's logged in on the other computer. 您需要为另一台计算机上登录的用户设置Windows身份验证。 Because you are using Integrated Security, it will attempt to connect to SQL Server using the windows login of the current user.
因为您使用的是Integrated Security,它将尝试使用当前用户的Windows登录名连接到SQL Server。 This will be a different login on different computers, so the SQL Server on the other computer needs to have that user added.
这将是不同计算机上的不同登录名,因此另一台计算机上的SQL Server需要添加该用户。
You need to start SQL Server Browser Service
. 您需要启动
SQL Server Browser Service
。 It lets your app connects to SQL Server: 它使您的应用程序连接到SQL Server:
SQL Server Browser listens for incoming requests for Microsoft SQL Server resources and provides information about SQL Server instances installed on the computer.
SQL Server浏览器侦听Microsoft SQL Server资源的传入请求,并提供有关计算机上安装的SQL Server实例的信息。
To enable it: 要启用它:
In SQL Server Configuration Manager, go to Properties => Service tab => Start Mode = Automatic. 在SQL Server配置管理器中,转到“属性=>服务选项卡=>启动模式=自动”。
Or 要么
This computer => Manage => Services and Applications => Services => SQL Server Broswer
这台计算机=>管理=>服务和应用程序=>服务=>
SQL Server Broswer
Note: You need a sql specific user for connect to it, you can not go with Integrated Security
for every machine. 注意:您需要一个特定于sql的用户来连接它,因此不能对每台计算机都使用
Integrated Security
。
Create a Sql Server user and allow it to access the Database: 创建一个Sql Server用户并允许其访问数据库:
--//Creates the login AbolrousHazem with password '340$Uuxwp7Mcxo7Khy'.
CREATE LOGIN AbolrousHazem
WITH PASSWORD = '340$Uuxwp7Mcxo7Khy';
GO
--//Creates a database user for the login created above.
CREATE USER AbolrousHazem FOR LOGIN AbolrousHazem;
GO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.