1

一部のサーバーに必要な SQL Server エイリアス エントリを自動的に作成する小さなアプリがあります。コードの大部分は次のようになります。

        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);
        }

これにより、32 ビット OS で必要なエントリが正しく作成されますが、x64 OS では、エイリアスを 64 ビット SQL Server クライアント構成にも追加する必要があります。

これを行う方法はありますか?

ありがとう。

4

2 に答える 2

3

実行可能であるため、レジストリの回答はそのまま残しますが、ConnectionOptions の Context を使用してアーチ (int、32、または 64) を指定できます。

64 ビットから両方にアクセスするサンプル:

    static void Main(string[] args)
    {
        var options = new ConnectionOptions();

        if (Environment.Is64BitOperatingSystem && Environment.Is64BitProcess == false)
        {
            Console.WriteLine("Please build as AnyCPU or x64");
            return;
        }

        // default behavior, should be 64-bit WMI provider
        Console.WriteLine("Print 64-bit aliases");
        PrintAliases(options);

        // specify the 32-bit arch
        Console.WriteLine("Print 32-bit aliases");
        options.Context.Add("__ProviderArchitecture", 32);
        PrintAliases(options);
    }

    private static void PrintAliases(ConnectionOptions options)
    {
        var scope = new ManagementScope(@"\\.\root\Microsoft\SqlServer\ComputerManagement10", options);
        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();
            var propertiesToRead = new[] { "AliasName", "ServerName", "ProtocolName", "ConnectionString" };
            foreach (var propertyToRead  in propertiesToRead)
            {
                Console.WriteLine("Property {0} = {1}", propertyToRead, existingAlias.GetPropertyValue(propertyToRead));
            }
        }
    }

32 ビットから両方にアクセスするサンプル (注: もちろん、プロセスのビット数に関係なく、アーチを強制的に 32 と 64 にすることができます)

class Program
{
    static void Main(string[] args)
    {
        var options = new ConnectionOptions();

        if (Environment.Is64BitProcess)
        {
            Console.WriteLine("Please run this sample as 32-bit");
            return;
        }

        // default behavior, should be 32-bit WMI provider since we build as x86
        Console.WriteLine("Print 32-bit aliases");
        PrintAliases(options);

        // also prints 32-bit aliases
        options.Context.Add("__ProviderArchitecture", 32);
        PrintAliases(options);

        // specify the 64-bit arch
        if (Environment.Is64BitOperatingSystem)
        {
            Console.WriteLine("Print 64-bit aliases");
            options.Context.Add("__ProviderArchitecture", 64);
            PrintAliases(options);
        }
    }

    private static void PrintAliases(ConnectionOptions options)
    {
        var scope = new ManagementScope(@"\\.\root\Microsoft\SqlServer\ComputerManagement10", options);
        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();
            var propertiesToRead = new[] { "AliasName", "ServerName", "ProtocolName", "ConnectionString" };
            foreach (var propertyToRead  in propertiesToRead)
            {
                Console.WriteLine("Property {0} = {1}", propertyToRead, existingAlias.GetPropertyValue(propertyToRead));
            }
        }
    }
于 2010-08-16T12:29:15.590 に答える
1

これを最後に調べたとき、クライアントエイリアスはレジストリ(HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ MSSQLServer \ Client \ ConnectTo)に永続化されていたため、最も簡単なルートは両方のWoW(HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \ Microsoft \ MSSQLServer \ Client \ ConnectTo)およびx64で実行している場合の「通常の」場所。32ビットプロセスとして実行している場合は、64ビットバージョンを作成するときに、p / invokeするか、(。net 4の場合)64ビットビューを指定する必要があることに注意してください。

于 2010-08-16T01:53:06.667 に答える