Skip to content

Latest commit

 

History

History
60 lines (47 loc) · 4.08 KB

using-linked-servers-in-smo.md

File metadata and controls

60 lines (47 loc) · 4.08 KB
title description author ms.author ms.date ms.service ms.topic helpviewer_keywords monikerRange
Using Linked Servers in SMO
Using Linked Servers in SMO
markingmyname
maghan
08/06/2017
sql
reference
linked servers [SQL Server], SMO
=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current

Using Linked Servers in SMO

[!INCLUDE SQL Server ASDB, ASDBMI, ASDW]

A linked server represents an OLE DB data source on a remote server. Remote OLE DB data sources are linked to the instance of [!INCLUDEssNoVersion] by using the xref:Microsoft.SqlServer.Management.Smo.LinkedServer object.

Remote database servers can be linked to the current instance of [!INCLUDEmsCoName] [!INCLUDEssNoVersion] by using an OLE DB Provider. In SMO, linked servers are represented by the xref:Microsoft.SqlServer.Management.Smo.LinkedServer object. The xref:Microsoft.SqlServer.Management.Smo.LinkedServer.LinkedServerLogins%2A property references a collection of xref:Microsoft.SqlServer.Management.Smo.LinkedServerLogin objects. These store the logon credentials that are required to establish a connection with the linked server.

OLE-DB Providers

In SMO, installed OLE-DB providers are represented by a collection of xref:Microsoft.SqlServer.Management.Smo.OleDbProviderSettings objects.

Example

For the following code examples, you will have to select the programming environment, programming template and the programming language to create your application. For more information, see Create a Visual C# SMO Project in Visual Studio .NET.

Creating a link to an OLE-DB Provider Server in Visual C#

The code example shows how to create a link to a [!INCLUDEssNoVersion] OLE DB, heterogeneous data source by using the xref:Microsoft.SqlServer.Management.Smo.LinkedServer object. By specifying [!INCLUDEssNoVersion] as the product name, data is accessed on the linked server by using the [!INCLUDEssNoVersion] Client OLE DB Provider, which is the official OLE DB provider for [!INCLUDEssNoVersion].

//Connect to the local, default instance of SQL Server.   
{   
   Server srv = new Server();   
   //Create a linked server.   
   LinkedServer lsrv = default(LinkedServer);   
   lsrv = new LinkedServer(srv, "OLEDBSRV");   
   //When the product name is SQL Server the remaining properties are   
   //not required to be set.   
   lsrv.ProductName = "SQL Server";   
   lsrv.Create();   
}   

Creating a link to an OLE-DB Provider Server in PowerShell

The code example shows how to create a link to a [!INCLUDEssNoVersion] OLE DB, heterogeneous data source by using the xref:Microsoft.SqlServer.Management.Smo.LinkedServer object. By specifying [!INCLUDEssNoVersion] as the product name, data is accessed on the linked server by using the [!INCLUDEssNoVersion] Client OLE DB Provider, which is the official OLE DB provider for [!INCLUDEssNoVersion].

#Get a server object which corresponds to the default instance  
$svr = New-Object -TypeName Microsoft.SqlServer.Management.SMO.Server  
  
#Create a linked server object which corresponds to an OLEDB type of SQL Server product  
$lsvr = New-Object -TypeName Microsoft.SqlServer.Management.SMO.LinkedServer -argumentlist $svr,"OLEDBSRV"  
  
#When the product name is SQL Server the remaining properties are not required to be set.   
$lsvr.ProductName = "SQL Server"  
  
#Create the Database Object  
$lsvr.Create()