Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Please tell me why I failed with this connection...how should I do then?

  • local machine name = MYPC
  • local instance name = MSSQLSERVER
  • my own database named mydatabase
  • I use Windows authentication to log into the local server, username = DOMAIN\mypc
  • I have no password when I log into the local server;

Lines I used are here:

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

These lines failed with the following error:

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
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

A see a few problems with our connection string:

MSSQLSERVER is usually the service name for a default instance of SQL Server in which case you would use "server=MYPC" or you can use dot for the local machine "server=.". If you really have an instance name of MSSQLServer which is doubtful then your server you be "server=.\MSSQLSERVER"

When using Windows authentication i.e. trusted_connection you don't specify a uid and trusted_connection should be set to true. Here's what your connection should look like:

$Connection.ConnectionString ="server=.;database=mydatabase;trusted_connection=true;"
share|improve this answer
    
Thank you very much! I stucked here for 2 days! –  pansal Jul 6 '11 at 2:18
    
If you could mark question as answered--thanks –  Chad Miller Jul 6 '11 at 19:14
    
Of course~ I marked your answer now :) –  pansal Jul 8 '11 at 5:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.