通过PowerShell连接到本地SQL Server数据库失败,错误:命名pipe道提供程序,错误:40

请告诉我为什么我在这方面失败了,那我该怎么办呢?

  • 本地计算机名称= MYPC
  • 本地实例名称= MSSQLSERVER
  • 我自己的数据库名为mydatabase
  • 我使用Windows身份validationlogin到本地服务器,用户名= DOMAIN\mypc
  • login本地服务器时没有密码;

我使用的线在这里:

 $connection= new-object system.data.sqlclient.sqlconnection $connection $Connection.ConnectionString ="server=MSSQLSERVER;database=mydatabase;trusted_connection=false;uid=DOMAIN\mypc;" $Connection.ConnectionString $connection.open() 

这些行失败,出现以下错误:

 Exception calling "Open" with "0" argument(s): "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)" At line:1 char:17 + $connection.open <<<< () + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException 

看到我们的连接string的一些问题:

MSSQLSERVER通常是SQL Server的默认实例的服务名称,在这种情况下,您可以使用“server = MYPC”,也可以将dot用于本地计算机“server =。”。 如果你真的有一个实例名称MSSQLServer这是可疑的,那么你的服务器是“服务器=。\ MSSQLSERVER”

在使用Windows身份validation(即trusted_connection)时,不要指定uid,并且应将trusted_connection设置为true。 以下是您的连接应该是这样的:

 $Connection.ConnectionString ="server=.;database=mydatabase;trusted_connection=true;"