C# 如何从服务器资源管理器检索连接字符串
我想为VisualStudio编写一个扩展,它将使我能够为指定的表生成一个模型 我使用以下代码将MyCommand项添加到服务器资源管理器中表的上下文菜单中:C# 如何从服务器资源管理器检索连接字符串,c#,visual-studio,add-in,server-explorer,C#,Visual Studio,Add In,Server Explorer,我想为VisualStudio编写一个扩展,它将使我能够为指定的表生成一个模型 我使用以下代码将MyCommand项添加到服务器资源管理器中表的上下文菜单中: Commands2 commands = (Commands2)_applicationObject.Commands; CommandBar menuBarCommandBar = ((CommandBars)_applicationObject.CommandBars)["Object Node"]; Command command
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,为什么删除了我的问题 找到了解决办法。 习惯于
布拉德·拉森,为什么我的问题被删除了 找到了解决办法。 习惯于
为什么我的问题被删除,因为你把它作为一个答案张贴-谢谢这很清楚,但我找不到办法对问题发表评论,只有答案才有可能。一定是我弄错了什么。方法是问你自己的问题,可能有一个链接显示搜索的努力-就像你做的那样-总是一个好迹象你可以回答和评论你自己的问题,所以可以尝试在你的问题的评论中用@username吸引以前的海报。但从未尝试过。干杯,欢迎来到SO:-谢谢!在这两种情况下,用户都会被吸引-我更喜欢更简单的方式=嗯。。。更简单的方法是什么?正如你所经历的那样,那些问题或其他没有实际回答的答案会在发布后很快被删除-为什么我的问题被删除,因为你把它作为一个答案张贴-谢谢这很清楚,但我找不到办法对问题发表评论,只有答案才有可能。一定是我弄错了什么。方法是问你自己的问题,可能有一个链接显示搜索的努力-就像你做的那样-总是一个好迹象你可以回答和评论你自己的问题,所以可以尝试在你的问题的评论中用@username吸引以前的海报。但从未尝试过。干杯,欢迎来到SO:-谢谢!在这两种情况下,用户都会被吸引-我更喜欢更简单的方式=嗯。。。更简单的方法是什么?正如你所经历的那样,那些问题或其他没有实际回答的答案会在发布后很快被删除- 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;
}