Attach Database Using SMO keep Failing
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 g开发者_开发问答etting 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)"}
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):
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).
I had a similar issue. Could not attach database files via smo. But if I copy the mdf and ldf files from the debug or release folders of Visual studio to say the root directory (C:), they attach programatically well from there. In addition to not accessing the server, you may try moving the db files to another location. Hope that helps
精彩评论