简体   繁体   English

C#使用exe连接到SQL Server

[英]C# Connecting to SQL Server using exe

My goal is to create a stand-alone C# Windows form application which will connect to SQL Server. 我的目标是创建一个独立的C#Windows窗体应用程序,该应用程序将连接到SQL Server。

I have to deploy this stand-alone file in different client machine. 我必须将此独立文件部署在其他客户端计算机上。

Consider I have two machines - Machine A and B. 考虑我有两台机器-机器A和B。

On Machine A, I have installed SQL Server 2008 R2 and VS2013 for development. 在计算机A上,我已安装SQL Server 2008 R2和VS2013进行开发。

Connection string I have used for development: 我用于开发的连接字符串:

Data Source=AssetName\SQLEXPRESS;Initial Catalog=Test_Database;Integrated Security=True

which is working fine on Machine A. When I tried the same on Machine B it is not connecting to SQL Server on Machine A. 在机器A上正常工作。当我在机器B上尝试相同操作时,它未连接到机器A上的SQL Server。

When I searched on internet, I got many references suggesting to use service-based database in VS. 当我在Internet上搜索时,我得到了很多建议,建议在VS中使用基于服务的数据库。 However I am finding difficult to understand how service-based database is working. 但是,我发现很难理解基于服务的数据库的工作方式。 I couldn't see any connectivity between the server and service-based database. 我看不到服务器和基于服务的数据库之间的任何连接。

Can anyone help me with this? 谁能帮我这个?

Also please let me know to connect the SQL Server on Machine A do I need to use SQL Server Management Studio on Machine B? 另外,请让我知道要在计算机A上连接SQL Server,我是否需要在计算机B上使用SQL Server Management Studio?

Error msg: 错误消息:

System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while extablishing a connection to SQL Server. System.Data.SqlClient.SqlException(0x80131904):建立与SQL Server的连接时发生与网络有关或特定于实例的错误。 The server was not found or was not accessible. 服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。

try to configure your server in machine A to allow access from other machines. 尝试在计算机A中配置服务器以允许从其他计算机进行访问。 generally you have to use your machine A adress ip as a server name with sql server instance name to connect from other machine. 通常,您必须将计算机A地址ip与sql服务器实例名称一起用作服务器名称,才能从其他计算机连接。

SQL Server Express doesn't allow remote connections by default - you need to explicitly enable this feature. 默认情况下,SQL Server Express不允许远程连接-您需要显式启用此功能。

To do so, use these steps: 为此,请使用以下步骤:

  • in SQL Server Management Studio, go to the Object Explorer 在SQL Server Management Studio中,转到“对象资源管理器”
  • right-click on the top-level node (which represents the SQL Server Express instance - usually labeled ......\\SQLEXPRESS ) 右键单击顶级节点(代表SQL Server Express实例-通常标记为......\\SQLEXPRESS
  • in the Properties dialog, go to the Connections tab on the left side 在“ Properties对话框中,转到左侧的“ Connections选项卡
  • check the option checkbox Allow remote connectìons 选中选项复选框Allow remote connectìons

在此处输入图片说明

  • restart your SQL Server Express instance 重新启动您的SQL Server Express实例

Now you should be able to connect to this SQL Server Express instance from a remote machine using MACHINENAME\\SQLEXPRESS as the server/instance name. 现在,您应该能够使用MACHINENAME\\SQLEXPRESS作为服务器/实例名称从远程计算机连接到此SQL Server Express实例。

Try to connect with ready proven MS-SQL client, I use MS SQL Management Studio. 尝试与已验证的MS-SQL客户端连接,我使用MS SQL Management Studio。
Connection window has few options, You can check ie firewall is blocking access, "broadcast" naming service is active etc. 连接窗口有几个选项,您可以检查防火墙是否阻止访问,“广播”命名服务处于活动状态等。
Maybe problem is not at client side, but server side (TCP-IP disabled, non listening on IP range ... many more situations). 也许问题不在客户端,而在服务器端(TCP-IP禁用,不侦听IP范围...更多情况)。

Who is administrator of SQL Server? 谁是SQL Server的管理员? What with 1433 port? 1433端口怎么办? Is TCP-IP enabled? 是否启用了TCP-IP? Default on most versions is not. 大多数版本的默认设置不是。

On next stage Management will be usefull ie to design queries etc. 在下一阶段,管理将很有用,例如设计查询等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM