如何将新的查询脚本与 SSMS 加载项连接?
我正在尝试创建 SSMS 加载项。 我想做的一件事是创建一个新的查询窗口并以编程方式将其连接到服务器实例(在 SQL 登录的上下文中)。 我可以很好地创建新的查询脚本窗口,但如果不先手动连接到其他东西(例如对象资源管理器),我就无法找到如何连接它。
换句话说,如果我手动将 Obect Explorer 连接到 SQL 实例,然后执行创建查询窗口的外接程序的方法,我可以使用以下代码连接它:
ServiceCache.ScriptFactory.CreateNewBlankScript(
Editors.ScriptType.Sql,
ServiceCache.ScriptFactory.CurrentlyActiveWndConnectionInfo.UIConnectionInfo,
null);
但我不想依赖 CurrentActiveWndConnectionInfo.UIConnectionInfo用于连接。 我想以编程方式设置 SQL 登录用户名和密码。
有人有什么想法吗?
编辑:
我已成功通过将最后一个参数设置为 System.Data.SqlClient.SqlConnection 的实例来连接查询窗口。 但是,连接使用上次连接的登录的上下文,而不是我尝试以编程方式设置的上下文。 也就是说,它连接的用户是在单击“新建查询”按钮并且没有连接对象资源管理器时在连接对话框中选择的用户。
编辑2:
我正在编写(或希望编写)一个加载项,以便在针对我们的生产服务器运行时自动将 SQL 语句和执行结果发送到我们的案例跟踪系统。 我的一个想法是删除写入权限并通过此加载项分配登录名,这也将强制用户输入一个案例#取消该语句(如果它不存在)。 我刚刚想到的另一个想法是检查 ServiceCache.ScriptFactory.CurrentlyActiveWndConnectionInfo.UIConnectionInfo 中的服务器名称,并将其与我们的生产服务器列表进行比较。 如果匹配并且没有 case # 则取消查询。
I'm trying to create a SSMS add-in. One of the things I want to do is to create a new query window and programatically connect it to a server instance (in the context of a SQL Login). I can create the new query script window just fine but I can't find how to connect it without first manually connecting to something else (like the Object Explorer).
So in other words, if I connect Obect Explorer to a SQL instance manually and then execute the method of my add-in that creates the query window I can connect it using this code:
ServiceCache.ScriptFactory.CreateNewBlankScript(
Editors.ScriptType.Sql,
ServiceCache.ScriptFactory.CurrentlyActiveWndConnectionInfo.UIConnectionInfo,
null);
But I don't want to rely on CurrentlyActiveWndConnectionInfo.UIConnectionInfo for the connection. I want to set a SQL Login username and password programatically.
Does anyone have any ideas?
EDIT:
I've managed to get the query window connected by setting the last parameter to an instance of System.Data.SqlClient.SqlConnection. However, the connection uses the context of the last login that was connected instead of what I'm trying to set programatically. That is, the user it connects as is the one selected in the Connection Dialog that you get when you click the New Query button and don't have an Object Explorer connected.
EDIT2:
I'm writing (or hoping to write) an add-in to automatically send a SQL statement and the execution results to our case-tracking system when run against our production servers. One thought I had was to remove write permissions and assign logins through this add-in which will also force the user to enter a case # canceling the statement if it's not there. Another thought I've just had is to inspect the server name in ServiceCache.ScriptFactory.CurrentlyActiveWndConnectionInfo.UIConnectionInfo and compare it to our list of production servers. If it matches and there's no case # then cancel the query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以防万一其他人正在寻找一种以编程方式创建和连接查询窗口的方法 - SSMS 2012 的一个简短示例:
此代码片段将打开一个使用 Windows 身份验证连接到指定服务器和数据库的新查询窗口。
Just in case anybody else is searching for a way to create and connect a query window programmaticaly - a short example for SSMS 2012:
This code snippet will open a new query window connected to specified server and database with windows auth.
我还没有找到执行此操作的方法,因为似乎无法连接到连接对话框窗口。
你在做什么?
编辑:
如果我理解正确,您想拦截正在运行的查询,如果它与生产服务器匹配,则取消它,否则将文本和结果发送到数据库?
嗯...虽然这是可能的,但背后确实是一个巨大的痛苦,我不会为此使用加载项。 另外,可以禁用、卸载加载项等。
您最好尝试在生产服务器上进行适当的安全设置来执行此操作。
I have not found a way to do this, since there appears to be no way to hook into the connection dialog window.
what are you working on?
EDIT:
If i understand correctly you want to intercept the query being run and if it matches the production server cancel it else send the text and results to a db?
hmm... while this would be possible but is a real major pain in the behind, and i wouldn't use an add-in for this. plus an add-in can be disabled, uninstalled etc.
you better try doing this with proper security setup on your production server.
我还没有尝试过,但不知道你是否尝试过。
应该允许您建立自己的连接。 但就像我说的,我还没有测试过。
您需要引用
Microsoft.SqlServer.RegSvrEnum.dll
。让我知道它是否有效。
I haven't tried this yet but didn't know if you have given it a shot.
Should allow you to make your own connection. But like I said, I haven't tested it yet.
You'll need a reference to
Microsoft.SqlServer.RegSvrEnum.dll
.Let me know if it works.