2015-05-22 63 views
1

您好我正在编写一个简单的控制台应用程序来使用批量复制将XML数据保存到数据库,但目前有问题连接到我的数据库。错误:40 - 无法打开连接到SQL Server

我得到一个错误的SQLException:

A network-related or instance-specific error occurred while establishing a connection to 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. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

我已经检查,以确保我的SQL服务器正在运行,并且它的正确配置的问题。以下是我的连接字符串。

string connectionString = @"Data Source=SYAZANA;Initial Catalog=WangSCM;integrated security=true;" ; 

我在我的智慧结束试图找出错误,那是什么我做了错误?

+0

SQL Server与程序是否在同一台机器上?如果没有,请检查SQL Server端的防火墙。至少应打开端口1433(默认实例)。 https://support.microsoft.com/de-ch/kb/968872/en-us SQL Server使用的更多端口 – Scoregraphic

+0

@Scoregraphic感谢您的答复,是的,他们在同一台机器上,我已经检查这是端口1433 – zana

+0

@zana请upvote并接受答案,如果它为你 –

回答

2

以下步骤:

*转到SQL Server配置管理器

* SQL Server网络配置:

*协议MSSQLSERVER

*在右窗格中拆分页面您将不得不关闭VIA,如下所示

--->共享内存 - 启用

--->命名管道 - 启用

---> TCP/IP - 启用

---> VIA - 禁用

或尝试以下

转到开始 - >搜索类型。在那里你会找到你的系统中可用的服务。

有寻找 - >SQL EXPRESS/SQL Server (SQLEXPRESS) - >右键点击它 - >然后开始它。 如果它已经在开始。只需停止开始或简单地重新启动它。

+0

我已经尝试过,但它没有工作 – zana

+0

看到**编辑** @junshoo – Balaji

+0

我刚刚重新启动它,但它仍然不成功:( – zana

0

我有同样的问题,由于某种原因,我不得不添加标准的SQL端口(1433),然后再次运行。你的情况,这将是:

string connectionString = @"Data Source=SYAZANA,1433;Initial Catalog=WangSCM;integrated security=true;" 
1

我发现的有用以下技术:

  1. 确保数据库引擎配置为接受远程连接
 
• Start > All Programs > SQL Server 2005 > Configuration Tools > SQL Server Surface Area Configuration 
• Click on Surface Area Configuration for Services and Connections 
• Select the instance that is having a problem > Database Engine > Remote Connections 
• Enable local and remote connections 
• Restart instance 
  • 检查SQL Server服务帐户
  •  
    • If you are not using a domain account as a service account (for example if you are using NETWORK SERVICE), you may want to switch this first before proceeding 
    
  • 如果您使用的是名为SQL Server实例,请确保你在你的连接字符串在ASP.NET Web应用程序使用实例名称
  •  
    • Usually the format needed to specify the database server is machinename\instancename 
    • Check your connection string as well 
    • If SQL Server is not configured on the default port (1433) then mention that port in the connection string 
    
  • 您可能需要创建防火墙中为SQL Server实例和端口的异常使用的是
  •  
    • Start > Run > Firewall.cpl 
    • Click on exceptions tab 
    • Add the sqlservr.exe (typically located in C:\Program Files (x86)\Microsoft SQL Server\MSSQL.x\MSSQL\Binn), and port (default is 1433) 
    • Check your connection string as well 
    
    1. 检查您的SQL服务器服务是否正常运行。

    2. 检查您是否连接到SQL Server。请注意您用于连接的内容:机器名称,域名或IP地址?检查连接时使用此。例如,如果使用的是MYSERVER

     
    • Start > Run > cmd 
    • netstat -ano| findstr 1433 
    • telnet myserver 1433 
    • ping myserver 
    
  • 启用TCP/IP在SQL Server配置
  •  
    • Go to All Programs >> Microsoft SQL Server 2008 >> Configuration Tools >> SQL Server Configuration Manager >> Select TCP/IP 
    • Right Click on TCP/IP >> Click on Enable 
    • You must restart SQL Server Services for all the changes to take effect. 
    
  • 配置SQL服务器端口号。
  •  
    • Open "SQL Server Configuration Manager" 
    • Click on "SQL Server Network Configuration" and Click on "Protocols for Name" 
    • Right Click on "TCP/IP" (make sure it is Enabled) Click on Properties 
    • Select "IP Addresses" Tab -and- Go to the last entry "IP All" 
    • Enter "TCP Port" 1433. 
    
    相关问题