2012-03-15 61 views
2

我想寫一個擴展到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); 

我怎樣才能獲取有關數據庫連接的信息?

回答

0

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; }

+0

_why我的問題是deleted_因爲你張貼作爲一個答案;-) – kleopatra 2012-11-11 11:23:24

+0

感謝。很明顯,但我找不到問題發表評論的方法(它只能用於答案)。一定是我困惑的東西。 – 2012-11-12 06:15:45

+1

要走的路是問你自己的問題,可能與此鏈接(顯示搜索努力 - 就像你做的一樣)總是一個好兆頭。你可以回答和評論你自己的問題,所以可能試圖吸引在你的問題的評論中,以前的海報與@username。雖然從未嘗試過。歡呼聲,歡迎來到SO :-) – kleopatra 2012-11-12 10:28:03

相關問題