I have a small app which automatically creates necessary SQL Server Alias entries for some servers. The bulk of the code looks like this:
private static void SetAlias(string aliasName, string server, string protocol, int? port)
{
var scope = new ManagementScope(@"\\.\root\Microsoft\SqlServer\ComputerManagement10");
try
{
scope.Connect();
}
catch
{
scope = new ManagementScope(@"\\.\root\Microsoft\SqlServer\ComputerManagement");
}
var clientAlias = new ManagementClass(scope, new ManagementPath("SqlServerAlias"), null);
clientAlias.Get();
foreach (ManagementObject existingAlias in clientAlias.GetInstances())
{
existingAlias.Get();
if (String.Equals((String)existingAlias.GetPropertyValue("AliasName"), aliasName))
{
UpdateAlias(existingAlias, aliasName, server, protocol, port);
return;
}
}
// create new
ManagementObject newAlias = clientAlias.CreateInstance();
UpdateAlias(newAlias, aliasName, server, protocol, port);
newAlias.Put();
}
private static void UpdateAlias(ManagementObject alias, string aliasName, string server, string protocol, int? port)
{
alias.SetPropertyValue("AliasName", aliasName);
alias.SetPropertyValue("ServerName", server);
alias.SetPropertyValue("ProtocolName", protocol);
alias.SetPropertyValue("ConnectionString", port != null ? port.ToString() : string.Empty);
}
This correctly creates the entries I want on 32bit OS's, however on x64 OS's I need the aliases also added to the 64 bit SQL Server Client Configuration.
Any ideas how to do this?
Thanks.