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

时间:2020-12-22 03:35:48

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 ?

我正在尝试为定制审计需求编写一个SSMS插件(需要审计生产环境中用户运行的所有查询)。我在适当的文件夹中有.addin文件,它会击中我的连接中的断点。Exec方法和我可以从活动文档中获取所选查询语句。但是我不确定是否有可以查找的属性或方法来获取用户连接的数据库名和服务器?

2 个解决方案

#1


5  

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) ..

一些通过codeplex浏览4个小时,下载每个项目和分析代码给了我需要的答案。我希望这对某个人有帮助(尽管我同意@Mitch,如果SQL Server审计对你有用,你应该先尝试一下)。

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.

Microsoft.SqlServer.RegSrvrEnum添加引用。dll和SqlWorkBench。接口(位于您的C:\编程文件中的某处)\ SQL Server . .-)。确保已经为这些工具安装了SDK。我只在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"]);

#2


1  

...need to audit all queries ran by users in a production environment

…需要审计生产环境中用户运行的所有查询。

That's what SQL Server Audit was designed for (SQL Server 2008 onwards):

这就是SQL Server Audit设计的目的(SQL Server 2008起):

Auditing an instance of the SQL Server Database Engine or an individual database involves tracking and logging events that occur on the Database Engine. SQL Server audit lets you create server audits, which can contain server audit specifications for server level events, and database audit specifications for database level events. Audited events can be written to the event logs or to audit files.

审计SQL Server数据库引擎或单个数据库的实例涉及跟踪和记录数据库引擎上发生的事件。SQL Server audit允许您创建服务器审计,它可以包含服务器级事件的服务器审计规范,以及数据库级事件的数据库审计规范。审计事件可以写入事件日志或审计文件。

Any auditing solution must run at the Database Engine, not clients (for obvious reasons!).

任何审计解决方案都必须在数据库引擎上运行,而不是在客户端上运行(原因显而易见!)

Also, normal users should have (at most) read access to Production via SSMS (via login/role permissions), thereby ensuring they can't change anything. That seems preferable to logging the fact after it happened.

此外,普通用户应该(最多)通过SSMS(通过登录/角色权限)读取产品访问权限,从而确保他们不能更改任何内容。这似乎比事后记录更可取。

#1


5  

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) ..

一些通过codeplex浏览4个小时,下载每个项目和分析代码给了我需要的答案。我希望这对某个人有帮助(尽管我同意@Mitch,如果SQL Server审计对你有用,你应该先尝试一下)。

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.

Microsoft.SqlServer.RegSrvrEnum添加引用。dll和SqlWorkBench。接口(位于您的C:\编程文件中的某处)\ SQL Server . .-)。确保已经为这些工具安装了SDK。我只在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"]);

#2


1  

...need to audit all queries ran by users in a production environment

…需要审计生产环境中用户运行的所有查询。

That's what SQL Server Audit was designed for (SQL Server 2008 onwards):

这就是SQL Server Audit设计的目的(SQL Server 2008起):

Auditing an instance of the SQL Server Database Engine or an individual database involves tracking and logging events that occur on the Database Engine. SQL Server audit lets you create server audits, which can contain server audit specifications for server level events, and database audit specifications for database level events. Audited events can be written to the event logs or to audit files.

审计SQL Server数据库引擎或单个数据库的实例涉及跟踪和记录数据库引擎上发生的事件。SQL Server audit允许您创建服务器审计,它可以包含服务器级事件的服务器审计规范,以及数据库级事件的数据库审计规范。审计事件可以写入事件日志或审计文件。

Any auditing solution must run at the Database Engine, not clients (for obvious reasons!).

任何审计解决方案都必须在数据库引擎上运行,而不是在客户端上运行(原因显而易见!)

Also, normal users should have (at most) read access to Production via SSMS (via login/role permissions), thereby ensuring they can't change anything. That seems preferable to logging the fact after it happened.

此外,普通用户应该(最多)通过SSMS(通过登录/角色权限)读取产品访问权限,从而确保他们不能更改任何内容。这似乎比事后记录更可取。