如何删除 SQL Server Management Studio 的“连接到服务器”中的服务器条目屏幕?

发布于 2024-11-11 16:02:38 字数 507 浏览 6 评论 0原文

可能的重复:
如何删除“服务器名称” ” SQL Server Management Studio 历史记录中的项目

在“连接到服务器”屏幕中,SQL Server Management Studio 存储您曾经输入的服务器名称、登录名和密码的所有条目。这非常有帮助,但有时情况会发生变化,服务器地址会发生变化,数据库不再可用。

如何从此屏幕删除服务器条目?此外,当您选择服务器时,列表中会显示过去的登录信息。再一次,这些发生了变化。如何删除用户条目?

连接到服务器屏幕

Possible Duplicate:
How to remove “Server name” items from history of SQL Server Management Studio

In the "Connect to Server" screen, SQL Server Management Studio stores all entries you have ever entered for Server Name, login and password. This is very helpful, but from time to time things change, servers addresses change, databases are no longer available.

How can I delete server entries from this screen? Also, when you select a server, past logins are available in the list. Once again, these change. How can I delete user entries?

Connect to Server screen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如日中天 2024-11-18 16:02:38

看起来此文件是 Microsoft.SqlServer.Management.UserSettings.SqlStudio 类的二进制序列化版本,该类在 Microsoft.SqlServer.Management.UserSettings, Version=10.0.0.0, Culture= 中定义。中性,PublicKeyToken=89845dcd8080cc91 程序集(位于c:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Microsoft.SqlServer.Management.UserSettings.dll >)。

凭借一点开发技能(Visual Studio 甚至 Powershell),您可以将此文件反序列化到原始类中,找到要删除的条目,然后将文件重新序列化回来。

这应该会给你一个想法(处理 .bin 文件的副本)...

var binaryFormatter = new BinaryFormatter();
var inStream = new MemoryStream(File.ReadAllBytes(@"c:\temp\SqlStudio.bin"));
var settings = (SqlStudio) binaryFormatter.Deserialize(inStream);
foreach (var pair in settings.SSMS.ConnectionOptions.ServerTypes)
{
    ServerTypeItem serverTypeItem = pair.Value;
    List<ServerConnectionItem> toRemove = new List<ServerConnectionItem>();
    foreach (ServerConnectionItem server in serverTypeItem.Servers)
    {
        if (server.Instance != "the server you want to remove")
        {
            continue;
        }
        toRemove.Add(server);
    }
    foreach (ServerConnectionItem serverConnectionItem in toRemove)
    {
        serverTypeItem.Servers.RemoveItem(serverConnectionItem);
    }
}

MemoryStream outStream = new MemoryStream();
binaryFormatter.Serialize(outStream, settings);
byte[] outBytes = new byte[outStream.Length];
outStream.Position = 0;
outStream.Read(outBytes, 0, outBytes.Length);
File.WriteAllBytes(@"c:\temp\SqlStudio.bin", outBytes);

在 Adrian 提出问题之后,我使用 Visual Studio 2010 在 Win7 x64 机器上再次尝试了这个。我发现了同样的错误,所以,在挖掘了一下之后我发现需要采取一些步骤才能解决。

  1. 在项目属性中将平台目标设置为“x86”,
  2. 添加对 Microsoft.SqlServer.Management.SDK.SqlStudio 的引用(在我的盒子上,该引用位于 c:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\ Common7\IDE\Microsoft.SqlServer.Management.Sdk.SqlStudio.dll)
  3. 添加对 Microsoft.SqlServer.Management.UserSettings 的引用(在与上一个相同的目录中)
  4. 执行自定义程序集解析

自定义程序集解析需要做一些工作因为(至少对我而言)不清楚为什么 CLR 不能正确解析程序集以及为什么 Visual Studio 不允许我手动添加引用。我说的是 SqlWorkbench.Interfaces.dll。

更新后的代码如下所示:

internal class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            var binaryFormatter = new BinaryFormatter();
            var inStream = new MemoryStream(File.ReadAllBytes(@"c:\temp\SqlStudio.bin"));
            var settings = (SqlStudio) binaryFormatter.Deserialize(inStream);
            foreach (var pair in settings.SSMS.ConnectionOptions.ServerTypes)
            {
                ServerTypeItem serverTypeItem = pair.Value;

                List<ServerConnectionItem> toRemove = new List<ServerConnectionItem>();
                foreach (ServerConnectionItem server in serverTypeItem.Servers)
                {
                    if (server.Instance != "the server you want to remove")
                    {
                        continue;
                    }
                    toRemove.Add(server);
                }
                foreach (ServerConnectionItem serverConnectionItem in toRemove)
                {
                    serverTypeItem.Servers.RemoveItem(serverConnectionItem);
                }
            }


            MemoryStream outStream = new MemoryStream();
            binaryFormatter.Serialize(outStream, settings);
            byte[] outBytes = new byte[outStream.Length];
            outStream.Position = 0;
            outStream.Read(outBytes, 0, outBytes.Length);
            File.WriteAllBytes(@"c:\temp\SqlStudio.bin", outBytes);
        }

        private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            Debug.WriteLine(args.Name);
            if (args.Name.StartsWith("SqlWorkbench.Interfaces"))
            {
                return Assembly.LoadFrom(@"C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\SqlWorkbench.Interfaces.dll");
            }
            return Assembly.Load(args.Name);
        }
    }

Looks like this file is a binary serialized version of the Microsoft.SqlServer.Management.UserSettings.SqlStudio class defined in the Microsoft.SqlServer.Management.UserSettings, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91 assembly (located at c:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Microsoft.SqlServer.Management.UserSettings.dll).

With a bit of development skill (Visual Studio or even Powershell) you can deserialize this file into the original class, find the entries you want to remove and re-serialize the file back out.

This should give you the idea (working on a copy of the .bin file)...

var binaryFormatter = new BinaryFormatter();
var inStream = new MemoryStream(File.ReadAllBytes(@"c:\temp\SqlStudio.bin"));
var settings = (SqlStudio) binaryFormatter.Deserialize(inStream);
foreach (var pair in settings.SSMS.ConnectionOptions.ServerTypes)
{
    ServerTypeItem serverTypeItem = pair.Value;
    List<ServerConnectionItem> toRemove = new List<ServerConnectionItem>();
    foreach (ServerConnectionItem server in serverTypeItem.Servers)
    {
        if (server.Instance != "the server you want to remove")
        {
            continue;
        }
        toRemove.Add(server);
    }
    foreach (ServerConnectionItem serverConnectionItem in toRemove)
    {
        serverTypeItem.Servers.RemoveItem(serverConnectionItem);
    }
}

MemoryStream outStream = new MemoryStream();
binaryFormatter.Serialize(outStream, settings);
byte[] outBytes = new byte[outStream.Length];
outStream.Position = 0;
outStream.Read(outBytes, 0, outBytes.Length);
File.WriteAllBytes(@"c:\temp\SqlStudio.bin", outBytes);

After Adrian's question, I tried this again on a Win7 x64 box using Visual Studio 2010. I found the same error so, after digging a bit I found it took a number of steps to resolve.

  1. Set the Platform target to 'x86' in the project properties
  2. add a reference to Microsoft.SqlServer.Management.SDK.SqlStudio (on my box this was at c:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Microsoft.SqlServer.Management.Sdk.SqlStudio.dll)
  3. add a reference to Microsoft.SqlServer.Management.UserSettings (in the same directory at the previous one)
  4. perform custom assembly resolution

The custom assembly resolution took a bit of doing since it wasn't obvious (to me, at least) why the CLR wouldn't just resolve the assembly correctly and why Visual Studio wouldn't allow me to add the reference manually. I'm talking about the SqlWorkbench.Interfaces.dll.

The updated code looks like this:

internal class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            var binaryFormatter = new BinaryFormatter();
            var inStream = new MemoryStream(File.ReadAllBytes(@"c:\temp\SqlStudio.bin"));
            var settings = (SqlStudio) binaryFormatter.Deserialize(inStream);
            foreach (var pair in settings.SSMS.ConnectionOptions.ServerTypes)
            {
                ServerTypeItem serverTypeItem = pair.Value;

                List<ServerConnectionItem> toRemove = new List<ServerConnectionItem>();
                foreach (ServerConnectionItem server in serverTypeItem.Servers)
                {
                    if (server.Instance != "the server you want to remove")
                    {
                        continue;
                    }
                    toRemove.Add(server);
                }
                foreach (ServerConnectionItem serverConnectionItem in toRemove)
                {
                    serverTypeItem.Servers.RemoveItem(serverConnectionItem);
                }
            }


            MemoryStream outStream = new MemoryStream();
            binaryFormatter.Serialize(outStream, settings);
            byte[] outBytes = new byte[outStream.Length];
            outStream.Position = 0;
            outStream.Read(outBytes, 0, outBytes.Length);
            File.WriteAllBytes(@"c:\temp\SqlStudio.bin", outBytes);
        }

        private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            Debug.WriteLine(args.Name);
            if (args.Name.StartsWith("SqlWorkbench.Interfaces"))
            {
                return Assembly.LoadFrom(@"C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\SqlWorkbench.Interfaces.dll");
            }
            return Assembly.Load(args.Name);
        }
    }
浪菊怪哟 2024-11-18 16:02:38

不幸的是,仅删除某些项目似乎不可能(或至少不可行)。

但是,如果需要,您可以重置配置并从头开始。

确保 Management Studio 已关闭,然后删除或重命名此文件:

%APPDATA%\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin

请注意,该文件包含其他用户首选项设置,因此,如果您自定义了 Management Studio 配置,则需要进行一些恢复工作。

参考: http:// Social.msdn.microsoft.com/Forums/en-US/sqltools/thread/94e5c3ca-c76d-48d0-ad96-8348883e8db8/

祝你好运!

Unfortunately, it does not appear to be possible (or at least practical) to only remove certain items.

However, if you want, you can reset the configuration and start from scratch.

Make sure Management Studio is closed, then delete or rename this file:

%APPDATA%\Microsoft\Microsoft SQL Server\100\Tools\Shell\SqlStudio.bin

Note that that file contains other user preference settings, so if you've customized your Management Studio configuration, you'll have some work to do restoring them.

Reference: http://social.msdn.microsoft.com/Forums/en-US/sqltools/thread/94e5c3ca-c76d-48d0-ad96-8348883e8db8/

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文