c# - 如何从服务器资源管理器检索 connectionStrings

标签 c# visual-studio add-in server-explorer

我想为 Visual Studio 编写一个扩展,这将使我能够为指定的表生成一个模型。

我使用以下代码将 MyCommand 项添加到服务器资源管理器中表的上下文菜单中:

Commands2 commands = (Commands2)_applicationObject.Commands;
CommandBar menuBarCommandBar = ((CommandBars)_applicationObject.CommandBars)["Object Node"];

Command command = commands.AddNamedCommand2(_addInInstance, "MyCommand", "MyCommand", 
    "Executes the command for MyCommand", true, 59, ref contextGUIDS,
    (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled,
    (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);
if ((command != null) && (menuBarCommandBar != null))
{
        command.AddControl(menuBarCommandBar, 1);
}

获取所选表格项的名称:

string fileName = "Dafault.cs";
var serverExplorer = _applicationObject.ToolWindows.GetToolWindow("Server Explorer") as UIHierarchy;
if (serverExplorer != null)
{
    dynamic item = ((object[])serverExplorer.SelectedItems)[0];
    fileName = string.Format("{0}.cs", item.Name);
}

//...
// Generate model based on table from database 
//...

_applicationObject.ItemOperations.NewFile("General\\Text File", fileName, Constants.vsViewKindCode);

如何获取有关数据库连接的信息?

最佳答案

Brad Larson,为什么我的问题被删除了?

找到解决方案。 二手this

public static IDbConnection GetConnection(DSRefNavigator navigator, out string type)
        {
            type = null;
            try
            {
                if (navigator != null)
                {
                    IVsDataConnectionsService dataConnectionsService =
                        (IVsDataConnectionsService) Package.GetGlobalService(typeof(IVsDataConnectionsService));
                    string itemName = navigator.GetConnectionName();

if (itemName != null) { int iConn; // = dataConnectionsService.GetConnectionIndex(itemName); DataViewHierarchyAccessor dataViewHierarchy = null; for(iConn = 0; iConn < dataConnectionsService.Count; iConn++) { DataViewHierarchyAccessor hierarchyAccessor = new DataViewHierarchyAccessor((IVsUIHierarchy) dataConnectionsService.GetConnectionHierarchy(iConn)); try { if (hierarchyAccessor.Connection.DisplayConnectionString == itemName) { dataViewHierarchy = hierarchyAccessor; break; } } catch { } } if (dataViewHierarchy != null) { DataConnection connection = dataViewHierarchy.Connection; if (connection != null && connection.ConnectionSupport.ProviderObject != null) { type = connection.ConnectionSupport.ProviderObject.GetType().FullName; return (IDbConnection) connection.ConnectionSupport.ProviderObject; } } } } } catch { } return null; }

关于c# - 如何从服务器资源管理器检索 connectionStrings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9718918/

相关文章:

c# - 有生产工厂的工厂可以吗?

C# SharpSSH 更改 SFTP 端口。

c# - 从 JSON 获取属性名称

sql-server - SSDT - 在架构创建时指定授权用户

c# - 无法将 DTE、项目或解决方案转换为 VCProject 和 VCCodeModel

c# - 这个事件处理代码会导致内存泄漏吗?

使用VS2008为64位windows平台编译C++静态库

C++|在函数外声明类

c# - 终止由Visual Studio插件生成的所有线程的正确方法是什么?

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