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

本文关键字:检索 connectionStrings 资源管理器 服务器 | 更新日期: 2023-09-27 18:27:49

我想为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);

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

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

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

找到了解决方案。使用了这个

公共静态IDbConnection GetConnection(DSRefNavigator导航器,输出字符串类型){type=null;尝试{if(navigator!=null){IVs数据连接服务数据连接服务=(IVsDataConnectionsService)Package.GetGlobalService((IVsDDataConnectionsService的类型));string itemName=导航器。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;
    }