views:

27

answers:

1

i use this SMO code to attach a database.

        Server mServer = new Server(".");

        mServer.AttachDatabase("DbName", new StringCollection { 
            "DbName.mdf", 
            "DbName_log.LDF" }, AttachOptions.None);

But i keep getting the exception

Failed to connect to server.

Any Ideas?

[EDITED]

Inner Exception:

{"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)"}

+1  A: 

The error says it all - SMO cannot connect to the server.

  • Does that local server exist?
  • Do you have the right to access that server??
  • Are you maybe using a named instance (e.g. ".\SQLExpress") ??

More points:

  • you don't specify a full path for the MDF/LDF file - try specifying a full path with directory and all. Does that make any difference?

Your code - as is - works flawlessly when I execute it locally on my machine. No problems at all ...... there must be something odd about your setup.

  • Is your SQL Server service up and running?

Your update would indicate that SMO is trying to use "named pipes" to communicate with your server. Is that protocol enabled on the server side? Check the SQL Server Configuration Manager (in the start menu; SQL Server > Configuration Tools > SQL Server Configuration Manager):

alt text

If the client tries to connect using named pipes, you need to make sure they're enabled on the server side, too (they're not, by default, as you can see from my screenshot).

marc_s
Local Server exists, i can attach the file by hand...Rights, i am the administrator of the computer.I just use the "." string on the Server constructor.
I used the full path with Path.GetFullPath static method, checked it too..it is full... ....\bin\Debug\data\DbName.mdf etc
Yes it is...if i attach the database by hand, my program starts correctly...when my program cant connect to the server i have a form setting conn string check connection and attach database...but keeps failing.
I have enabled all these yesterday cause i was testing clients in my network. Just checked its ok.