SSMS可扩展性/插件-获取当前数据库和服务器 [英] SSMS extensibility/addin - get current database and server
问题描述
我正在尝试为自定义审核要求编写SSMS插件(需要审核生产环境中用户运行的所有查询).我将.addin文件放在相应的文件夹中,并且它在Connect.Exec方法中命中了断点,并且能够从活动文档中选择查询语句.但是,我不确定是否可以查找任何属性或方法来获取数据库名称和用户连接到的服务器?
I am trying to write a SSMS addin for a custom auditing requirement (need to audit all queries ran by users in a production environment). I have the .addin file located in the appropriate folders and it hits the breakpoints in my Connect.Exec method and I am able to get the query statements under selection from the active document. However I am not sure if there is any property or method that I could look up to get the database name and the server the user was connected to ?
推荐答案
有些人在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"]);
这篇关于SSMS可扩展性/插件-获取当前数据库和服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!