2016-08-12 88 views
0

我使用它似乎無法連接到數據庫服務器,錯誤是26號SQL Server管理對象(SMO)有一個應用程序:爲什麼我不能使用SMO連接到SQL Server?

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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

然而,這似乎是唯一的事情,無法連接到數據庫。使用下面的作品都沒有問題連接到數據庫:

  • SQL Server Management Studio中
  • SQLCMD
  • System.Data.SqlClient.SqlConnection
  • ODBC
  • SQLPS模塊在PowerShell中

這是我正在使用的代碼,當我嘗試訪問數據庫時引發異常,並且據我所知,所有值都是wha牛逼他們應該是:

Server = new SMO.Server(
    new ServerConnection(
     new SqlConnectionStringBuilder 
     { 
      DataSource = ServerName, 
      InitialCatalog = DatabaseName, 
      IntegratedSecurity = true 
     }.ConnectionString)); 

Database = Server.Databases[DatabaseName]; 

我發現的唯一的事情是,當我運行sqlcmd -L沒有我的SQL Server實例的顯示出來。我基本上希望看到這樣的事情:

Servers: 
    SQL2008 
    SQL2012 
    SQL2014 
    SQL2016 

但是,所有這說的是:

Servers: 
     ;UID:Login ID=?;PWD:Password=?;Trusted_Connection:Use Integrated Security=?;*APP:AppName=?;*WSID:WorkStation ID=?; 

回答

0

解決了,忘了RTFM和習慣勢力的另一種情況。

基本上,我忘記了構造函數Server(string)需要實例名稱而不是連接字符串。所以它試圖連接到與我的連接字符串具有相同名稱的實例。

還有的其它構造Server(ServerConnection)所以我應該做的或者是這樣的:

Server = new SMO.Server(
    new ServerConnection(
     new SqlConnectionStringBuilder 
     { 
      DataSource = ServerName, 
      InitialCatalog = DatabaseName, 
      IntegratedSecurity = true 
     })); 

或者很簡單,這樣的:

Server = new SMO.Server(ServerName); 
相關問題