SQL Server 2012 SSMSで動作する「実行するすべてのクエリを保存する」何かを持っているという目標を達成するために、これを試してみると、これは私のマシンで仕事をしているようです/リファクタリング)
これは、クラスが置き換えられたAndreiのサンプルプロジェクトに基づいていConnect
ます。SSMSAddin2012プロジェクト CodePlexに上にも非常に便利です。
namespace SSMSAddin
{
using System;
using System.IO;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.SqlServer.Management.UI.VSIntegration;
public class Connect : IDTExtensibility2
{
private DTE2 application;
private CommandEvents executeSqlEvents;
public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
this.application = (DTE2)application;
this.executeSqlEvents = this.application.Events.CommandEvents["{52692960-56BC-4989-B5D3-94C47A513E8D}", 1];
this.executeSqlEvents.BeforeExecute += this.executeSQLEvents_BeforeExecute;
}
private void executeSQLEvents_BeforeExecute(string guid, int id, object customin, object customout, ref bool canceldefault)
{
try
{
Document document = ((DTE2)ServiceCache.ExtensibilityModel).ActiveDocument;
var textDocument = (TextDocument)document.Object("TextDocument");
string queryText = textDocument.Selection.Text;
if(string.IsNullOrEmpty(queryText))
{
EditPoint startPoint = textDocument.StartPoint.CreateEditPoint();
queryText = startPoint.GetText(textDocument.EndPoint);
}
DateTime now = DateTime.Now;
string folderPath = string.Format(@"E:\SSMS Queries\{0}", now.ToString("yyyyMMdd"));
string fileName = now.ToString("HHmmss-FFFFFFF") + ".sql";
Directory.CreateDirectory(folderPath);
string fullPath = Path.Combine(folderPath, fileName);
File.WriteAllText(fullPath, queryText);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
#region Other Interface Methods
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom) { }
public void OnStartupComplete(ref Array custom) { }
public void OnAddInsUpdate(ref Array custom) { }
public void OnBeginShutdown(ref Array custom) { }
#endregion
}
}