2016-08-12 91 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); 
相关问题