Tuesday, March 20, 2012

Add/Drop LinkedServers using SMO

Anyone used SMO in SQL Server 2005 to add or drop Linked Server. I couldn't
find any sample or information on that.
ThanksHello,
I've never done this, but you probably need to use the LinkedServer
class in the Microsoft.SqlServer.Management.Smo namespace. Set the
desired properties of the LinkedServer, then use the Create method. For
more informations, see:
http://msdn2.microsoft.com/microsof...rs
.aspx
http://msdn2.microsoft.com/ms162171(en-US,SQL.90).aspx
Razvan|||Thanks for the response.
I solved the problem. If anyone interested heres the code for
Adding/Dropping Linked Servers using SMO. I use this in a Class.
public bool AddLinkServer(string SourceServer, string
DestinationServer,string UID,string PWD)
{
try
{
LinkedServer DestSrv;
ServerConnection SrvConn = new ServerConnection();
SrvConn.ServerInstance = SourceServer;
if ((UID == "") && (PWD == ""))
{
SrvConn.LoginSecure = true;
}
else
{
SrvConn.LoginSecure = false;
SrvConn.Login = UID;
SrvConn.Password = PWD;
}
Server SQLServer = new Server(SrvConn);
DestSrv = new LinkedServer(SQLServer, DestinationServer);
// Add Remote user/password for linking.
LinkedServerLogin LnkLogin = new LinkedServerLogin(DestSrv, UID);
if (!SrvConn.LoginSecure)
{
LnkLogin.RemoteUser = UID;
LnkLogin.SetRemotePassword(PWD);
LnkLogin.Create();
}
DestSrv.Create();
SrvConn.Disconnect();
return true;
}
catch
{
return false;
}
}
// drop link serverss...
public bool DropLinkServer(string SourceServer,string DropLinkServerName)
{
try
{
ServerConnection SrvConn = new ServerConnection();
SrvConn.ServerInstance = SourceServer;
SrvConn.LoginSecure = true;
Server SQLServer = new Server(SrvConn);
LinkedServerCollection LnkServerList = SQLServer.LinkedServers;
foreach (LinkedServer Lnk in LnkServerList)
{
if (Lnk.Name == DropLinkServerName)
{
Lnk.Drop(true);
break;
}
}
SrvConn.Disconnect();
return true;
}
catch
{
return false;
}
}
// Load all linked servers...
public bool LoadLinkServers(string SourceServer, ArrayList LinkServerList)
{
try
{
ServerConnection SrvConn = new ServerConnection();
SrvConn.ServerInstance = SourceServer;
SrvConn.LoginSecure = true;
Server SQLServer = new Server(SrvConn);
LinkedServerCollection LnkSrvList = SQLServer.LinkedServers;
foreach (LinkedServer Lnk in LnkSrvList)
{
LinkServerList.Add(Lnk.Name);
}
SrvConn.Disconnect();
return true;
}
catch
{
return false;
}
}
"Razvan Socol" <rsocol@.gmail.com> wrote in message
news:1138559727.598122.180660@.g44g2000cwa.googlegroups.com...
> Hello,
> I've never done this, but you probably need to use the LinkedServer
> class in the Microsoft.SqlServer.Management.Smo namespace. Set the
> desired properties of the LinkedServer, then use the Create method. For
> more informations, see:
> http://msdn2.microsoft.com/microsof...
rs.aspx
> http://msdn2.microsoft.com/ms162171(en-US,SQL.90).aspx
> Razvan
>

No comments:

Post a Comment