从Visual Studio 2012插件创建连接的查询窗口
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从Visual Studio 2012插件创建连接的查询窗口相关的知识,希望对你有一定的参考价值。
我试图从Visual Studio 2012插件创建一个连接的查询窗口。
这是我正在使用的代码:
UIConnectionInfo u = new UIConnectionInfo
{
ServerName = serverName,
ServerType = new Guid(serverType),
AuthenticationType = authType
};
u.AdvancedOptions.Set("DATABASE", databaseName);
u.AdvancedOptions.Set("PACKET_SIZE", "4096");
u.AdvancedOptions.Set("CONNECTION_TIMEOUT", "15");
u.AdvancedOptions.Set("EXEC_TIMEOUT", "0");
u.AdvancedOptions.Set("ENCRYPT_CONNECTION", "False");
u.AdvancedOptions.Set("USE_CUSTOM_CONNECTION_COLOR", "False");
u.AdvancedOptions.Set("CUSTOM_CONNECTION_COLOR", "-986896");
u.ApplicationName = "Microsoft SQL Server Data Tools, T-SQL Editor";
u.UserName = userName;
if(authType == 1)
{
u.Password = password;
}
ScriptFactory scriptFactory = ScriptFactory.Instance;
if(scriptFactory != null)
{
scriptFactory.CreateNewBlankScript(ScriptType.Sql, u, null);
}
UIConnectionInfo中的所有属性都设置为从Visual Studio 2012手动打开查询窗口时的确切值。
scriptFactory.FileOpenMode设置为Connected。
我甚至尝试传递一个SQLConnection对象,该对象具有与CreateNewBlankScript的打开和关闭连接(这有助于Visual Studio 2010),但没有结果。
在其他程序集中,我引用了SQLEditors.dll和SQLWorkbench.Interfaces,这两个版本都是11.0。
任何信息都会非常有用。
谢谢。
答案
其中UIConnectionInfo和SqlConnection是您的连接和queryWindow当前打开的窗口
private static void SetConnectionForQueryWindow(Window queryWindow,
UIConnectionInfo uiConnectionInfo, SqlConnection conn)
{
object tempQueryWindow;
Dictionary<string, object> openedQueryWindows;
List<Window> temp = new List<Window> { queryWindow };
openedQueryWindows = GetAllOpenedQueryWindows2012(temp);
openedQueryWindows.TryGetValue(queryWindow.Document.FullName, out tempQueryWindow);
if(tempQueryWindow == null)
{
return;
}
Assembly sqlEditor = Assembly.LoadFrom(tempQueryWindow.GetType().Assembly.Location);
var sqlScriptEditorControl = new SqlEditorTabbedEditorPane(sqlEditor);
var displaySqlResultsTabControl = new DisplaySqlResultsTabControl2012(tempQueryWindow, sqlScriptEditorControl);
try
{
var connectionStrategyField =
displaySqlResultsTabControl.QueryExecutorWrapper.QueryExec.GetType()
.GetField("_connectionStrategy", BindingFlags.Instance | BindingFlags.NonPublic);
var connectionStrategy = connectionStrategyField.GetValue(displaySqlResultsTabControl.QueryExecutorWrapper.QueryExec);
var connection = connectionStrategy.GetType()
.GetField("_connectionInfo", BindingFlags.Instance | BindingFlags.NonPublic);
connection.SetValue(connectionStrategy, uiConnectionInfo);
var setDbConn = connectionStrategy.GetType()
.GetMethod("SetDbConnection", BindingFlags.NonPublic | BindingFlags.Instance);
object[] parametersArray = { conn };
setDbConn.Invoke(connectionStrategy, parametersArray);
}
catch(NullReferenceException nullReferenceException)
{
}
}
internal static Dictionary<string, object> GetAllOpenedQueryWindows2012(List<Window> windows)
{
Dictionary<string, object> openedQuerySqlWindows = new Dictionary<string, object>();
foreach(Window window in windows)
{
foreach(Window queryWindow in window.Document.ActiveWindow.Collection)
{
if(queryWindow.Object == null)
{
continue;
}
if(queryWindow.Object.ToString()
!= "Microsoft.VisualStudio.Data.Tools.SqlEditor.UI.TabbedEditor.SqlEditorTabbedEditorPane")
{
continue;
}
object sqlEditorTabbedEditorPane = queryWindow.Object;
if(!openedQuerySqlWindows.ContainsKey(queryWindow.Document.FullName))
{
openedQuerySqlWindows.Add(queryWindow.Document.FullName, sqlEditorTabbedEditorPane);
}
}
}
return openedQuerySqlWindows;
}
哪里
public class SqlEditorTabbedEditorPane
{
private readonly FieldInfo m_displaySqlResultsControl;
public SqlEditorTabbedEditorPane(Assembly sqlEditor)
{
Type sqlEditorTabbedEditorPane =
sqlEditor.GetType(
"Microsoft.VisualStudio.Data.Tools.SqlEditor.UI.TabbedEditor.SqlEditorTabbedEditorPane");
m_displaySqlResultsControl = sqlEditorTabbedEditorPane.GetField(
"_resultsControl",
BindingFlags.NonPublic | BindingFlags.Instance);
}
public FieldInfo DisplaySqlResultsControl
{
get
{
return this.m_displaySqlResultsControl;
}
}
}
public class DisplaySqlResultsTabControl2012
{
private readonly object m_displaySqlResultsTabControl;
private readonly QueryExecutor m_queryExecutor;
public DisplaySqlResultsTabControl2012(
object scriptEditor,
SqlEditorTabbedEditorPane sqlScriptEditorControl)
{
m_displaySqlResultsTabControl = sqlScriptEditorControl.DisplaySqlResultsControl.GetValue(scriptEditor);
m_queryExecutor = new QueryExecutor(m_displaySqlResultsTabControl);
}
public DisplaySqlResultsTabControl2012(object scriptEditor, SqlEditorTabbedEditorPane sqlScriptEditorControl)
{
m_displaySqlResultsTabControl = sqlScriptEditorControl.DisplaySqlResultsControl.GetValue(scriptEditor);
m_queryExecutor = new QueryExecutor(m_displaySqlResultsTabControl);
}
public object DisplaySqlResultsTabCtr
{
get
{
return m_displaySqlResultsTabControl;
}
}
public QueryExecutor QueryExecutorWrapper
{
get
{
return m_queryExecutor;
}
}
public class QueryExecutor
{
private readonly object m_queryExecutor;
public QueryExecutor(object displaySqlResultsTabControl)
{
FieldInfo queryExecutor = displaySqlResultsTabControl.GetType()
.GetField("_queryExecutor", BindingFlags.Instance | BindingFlags.NonPublic);
if(queryExecutor != null)
{
this.m_queryExecutor = queryExecutor.GetValue(displaySqlResultsTabControl);
}
}
public object QueryExec
{
get
{
return m_queryExecutor;
}
}
}
}
以上是关于从Visual Studio 2012插件创建连接的查询窗口的主要内容,如果未能解决你的问题,请参考以下文章
从 Visual Studio C# 和 SQL Compact(连接、选择、插入)开始?
Visual Studio Express 2012 无法添加到 Visual Studio Team Services 的连接