这个问题已经在这里有了答案:
已关闭8年。
在“连接到服务器”屏幕中,SQL Server Management Studio将存储您为服务器名,登录名和密码输入的所有条目。这非常有帮助,但有时情况会发生变化,服务器地址也会发生变化,数据库不再可用。
如何从此屏幕删除服务器条目?另外,当您选择服务器时,列表中将提供过去的登录信息。这些再次改变。如何删除用户条目?
最佳答案
看起来此文件是Microsoft.SqlServer.Management.UserSettings,Version = 10.0.0.0,Culture = neutral,PublicKeyToken = 89845dcd8080cc91程序集中定义的Microsoft.SqlServer.Management.UserSettings.SqlStudio
类的二进制序列化版本(位于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盒子上再次尝试了此操作。我发现了相同的错误,因此,在进行了一些挖掘之后,我发现它需要采取许多步骤来解决。
自定义程序集解析需要花点时间做,因为它(至少对我而言)不是很明显,为什么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);
}
}
关于sql-server - 如何在SQL Server Management Studio的 “Connect to Server”屏幕中删除服务器条目? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6230159/