나는 이것을 가지고 놀았을 것이라고 생각하고 SQL Server 2012 SSMS에서 작동하는 "실행중인 모든 쿼리를 저장합니다." / 리팩토링)
클래스가 교체 된 Andrei의 샘플 프로젝트 를 기반으로합니다 Connect
. Codeplex 의 SSMSAddin2012 프로젝트 도 매우 유용합니다.
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
}
}