如何从另一台机器连接到SQL数据库? [英] How can I connect to sql database from another machine?

查看:250
本文介绍了如何从另一台机器连接到SQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows身份验证模式下有一个带有sql server 2008数据库的c#应用程序。当我在我的系统中运行它时效果很好。

但是当我在网络中共享c#app时,我无法连接到数据库并且我得到错误(网络相关或实例......)。 br />
这是我的连接字符串:

 conn =  new  SqlConnection( @  Server = localhost; Database = OfficialAutomation; Integrated Security = true;); 



我该怎么办?

感谢先进。

解决方案

尝试将localhost替换为机器的IP地址:

 conn =  new  SqlConnection( @  Server = XXX.XXX.XXX.XXX; Database = OfficialAutomation; Integrated Security = true;); 



我认为登录的用户可以访问网络上的机器吗?



否则,你可能想要o切换到SQL Server身份验证...


Server = localhost 表示SQL实例位于当前的PC上 - 它是它不是,它位于您的开发机器上(这就是为什么它在您编写时工作的原因)。关于此,您可以做两件事:

1)更改连接字符串以引用开发机器的绝对地址。这是相对简单的:

在VS中使用Server Explorer窗格设置连接:

1.1)打开服务器资源管理器。

1.2)右单击数据连接并选择添加连接

1.3)在随后的对话框中,选择您的数据源和数据库,指定安全信息,然后按测试连接按钮。 />
1.4)连接工作时,按确定

1.5)在服务器资源管理器窗格中突出显示数据库,然后查看属性窗格。将显示连接字符串的工作示例,您可以将其复制并粘贴到应用程序或配置文件中。这将是绝对的pc / instance而不是localhost相关。



2)设置第二台机器作为生产的SQL服务器,并使用相反,该实例的绝对地址。



就个人而言,我会使用第二种 - 当你继续处理应用程序时,你不会意外地影响生产数据库。想象一下,如果忘记将WHERE子句添加到UPDATE或DELETE语句中,可能会造成麻烦!



BTW:你还应该花时间移动你的连接字符串从您在问题中显示的硬编码值到基于设置的配置文件(基于某些描述),这样您就可以在生产和开发SQL实例之间切换PC而无需更改软件 - 这使得它更可靠,并有助于确保当你升级他们的软件时,用户不会切换回开发服务器并弄乱一切! :笑:

I have a c# application with sql server 2008 database in windows authentication mode. when i run it in my system it works well.
But when i share c# app in network, I can not connect to database and i get error( a network related or instance...).
this is my connection string:

conn = new SqlConnection(@"Server=localhost;Database=OfficialAutomation;Integrated Security=true;");


What should i do?
Thanks in advanced.

解决方案

Try replacing "localhost" with the IP address of the machine:

conn = new SqlConnection(@"Server=XXX.XXX.XXX.XXX;Database=OfficialAutomation;Integrated Security=true;");


I assume that the user that is logged in can access the machine on the network?

Otherwise, you might want to switch to SQL Server authentication...


Server=localhost says that the SQL instance is located on the current PC - which it isn''t, it is located on your development machine (which is why it worked when you wrote it). There are two things you can do about this:
1) Change the connection string to refer to the absolute address of your developemnt machine. This is relatively trivial:
Set up a connection in VS with the Server Explorer pane:
1.1) Open Server Explorer.
1.2) Right click "Data connections" and select "Add connection"
1.3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
1.4) When the connection works, press "OK"
1.5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file. This will be absolute "pc/instance" rather than "localhost" related.
Or
2) Set up a second machine as an SQL server for production, and use the absolute address of that instance instead.

Personally, I would use the second - that way when you continue work on the application, you will not accidentally affect the production database. Image the mess you could make if you forget to add the WHERE clause to an UPDATE or DELETE statement!

BTW: You should also take the time to move your connection string from a hardcoded value as you show in the question to a configuration file (of some description) based setting, so that you can switch your PC between production and development SQL instances without changing the software - it makes it more reliable, and helps to ensure that when you upgrade their software, the users don''t switch back to the development server and mess everything up! :laugh:


这篇关于如何从另一台机器连接到SQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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