gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:34:09 26 4
gpt4 key购买 nike

我想为 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com