且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SSMS可扩展性/插件-获取当前数据库和服务器

更新时间:2023-02-02 20:05:16

有些人在Codeplex中浏览了4个小时,下载了每个项目并分析了代码,这给了我所需的答案.我希望这有一天能对某人有所帮助(尽管我同意@Mitch,但如果SQL Server Audit为您工作,您应该先尝试一下)..

Some skimming through codeplex for 4 hours, downloading each project and analyzing the code gave me the answer I need. I hope this helps someone someday (although I agree with @Mitch if SQL Server Audit works for you, you should try that out first) ..

添加对Microsoft.SqlServer.RegSrvrEnum.dll和SqlWorkBench.Interfaces(位于C:\ ProgramFiles .. \ SQL Server ..-的某个位置)的引用.确保已安装工具的SDK.我仅在SQL Server Management Studio 2014中对此进行了测试.

Add reference to Microsoft.SqlServer.RegSrvrEnum.dll and SqlWorkBench.Interfaces (located somewhere in your C:\ProgramFiles..\SQL Server.. -). Make sure you have installed the SDK for the tools. I have only tested this for SQL Server Management Studio 2014.

然后下面的代码应该可以解决问题(欢迎您!)

Then the below code should do the trick (your welcome!)

IScriptFactory scriptFactory = ServiceCache.ScriptFactory;
CurrentlyActiveWndConnectionInfo connectionIfno = scriptFactory.CurrentlyActiveWndConnectionInfo;
UIConnectionInfo conn = connectionIfno.UIConnectionInfo;
Debug.WriteLine("{0}::{1}", conn.ServerName, conn.AdvancedOptions["DATABASE"]);