Skip to content

Latest commit

 

History

History
65 lines (51 loc) · 4.26 KB

File metadata and controls

65 lines (51 loc) · 4.26 KB
title Using Linked Servers in SMO | Microsoft Docs
ms.custom
ms.date 08/06/2017
ms.prod sql
ms.prod_service database-engine
ms.reviewer
ms.technology
ms.topic reference
helpviewer_keywords
linked servers [SQL Server], SMO
ms.assetid 0ea8837b-2596-4df1-b065-3bb717c9f22c
author markingmyname
ms.author maghan
monikerRange =azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||=sqlallproducts-allversions||>=sql-server-linux-2017||=azuresqldb-mi-current

Using Linked Servers in SMO

[!INCLUDEappliesto-ss-asdb-asdw-xxx-md]

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()