using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using SQLDMO;
using System.Threading;
using SourceSafeTypeLib;
// Project : DBScriptSafe
// File : DBScriptSafe.cs
// Author : Sriram Chitturi (c) 2004
// Date : May 06, 2004
namespace DBScriptSafe
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class DBScriptSafe : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ListView DBListView;
private System.Windows.Forms.Button SelectAllBtn;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button ClearAllBtn;
private System.Windows.Forms.Button CloseBtn;
private System.Windows.Forms.Button ConnectBtn;
private System.Windows.Forms.Button GetServersBtn;
private System.Windows.Forms.ComboBox ServerList;
private System.Windows.Forms.TextBox StatusText;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox DBLoginTxt;
private System.Windows.Forms.TextBox DBPwdTxt;
private System.Windows.Forms.TextBox VSSIniPath;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox VSSLoginTxt;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.TextBox VSSPwdTxt;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.GroupBox DBGroupBox;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button ScriptDBBtn;
private System.Windows.Forms.TextBox VSSProjTxt;
private SQLDMO.SQLServerClass m_sqlServer = null;
private ArrayList m_dbList = new ArrayList();
private VSSDatabaseClass m_vssDB = null;
private ScriptEngine m_scriptEngine = null;
private Thread m_scriptEngineThread = null;
private System.Windows.Forms.TextBox CopyRight;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public DBScriptSafe()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
DBListView.Columns.Add("Select Databases to script",
DBListView.Width-5, HorizontalAlignment.Left);
ScriptEngine.s_transfer.StatusMessage +=
new TransferSink_StatusMessageEventHandler(s_transfer_StatusMessage);
ScriptEngine.s_transfer.ScriptTransferPercentComplete +=
new TransferSink_ScriptTransferPercentCompleteEventHandler(s_transfer_ScriptTransferPercentComplete);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(DBScriptSafe));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.DBListView = new System.Windows.Forms.ListView();
this.SelectAllBtn = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.DBLoginTxt = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.DBPwdTxt = new System.Windows.Forms.TextBox();
this.ClearAllBtn = new System.Windows.Forms.Button();
this.CloseBtn = new System.Windows.Forms.Button();
this.ConnectBtn = new System.Windows.Forms.Button();
this.ServerList = new System.Windows.Forms.ComboBox();
this.GetServersBtn = new System.Windows.Forms.Button();
this.StatusText = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.VSSIniPath = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label7 = new System.Windows.Forms.Label();
this.VSSLoginTxt = new System.Windows.Forms.TextBox();
this.label8 = new System.Windows.Forms.Label();
this.VSSPwdTxt = new System.Windows.Forms.TextBox();
this.label9 = new System.Windows.Forms.Label();
this.VSSProjTxt = new System.Windows.Forms.TextBox();
this.DBGroupBox = new System.Windows.Forms.GroupBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.ScriptDBBtn = new System.Windows.Forms.Button();
this.CopyRight = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.Control;
this.label1.Location = new System.Drawing.Point(28, 68);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(46, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Server :";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// label2
//
this.label2.BackColor = System.Drawing.SystemColors.Control;
this.label2.Location = new System.Drawing.Point(14, 142);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(60, 16);
this.label2.TabIndex = 2;
this.label2.Text = "Database :";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// DBListView
//
this.DBListView.CheckBoxes = true;
this.DBListView.FullRowSelect = true;
this.DBListView.GridLines = true;
this.DBListView.Location = new System.Drawing.Point(78, 142);
this.DBListView.Name = "DBListView";
this.DBListView.Size = new System.Drawing.Size(312, 130);
this.DBListView.TabIndex = 3;
this.DBListView.View = System.Windows.Forms.View.Details;
//
// SelectAllBtn
//
this.SelectAllBtn.Cursor = System.Windows.Forms.Cursors.Hand;
this.SelectAllBtn.Location = new System.Drawing.Point(104, 276);
this.SelectAllBtn.Name = "SelectAllBtn";
this.SelectAllBtn.Size = new System.Drawing.Size(114, 22);
this.SelectAllBtn.TabIndex = 4;
this.SelectAllBtn.Text = "Select All";
this.SelectAllBtn.Click += new System.EventHandler(this.OnSelectAllDatabases);
//
// label3
//
this.label3.BackColor = System.Drawing.SystemColors.Control;
this.label3.Location = new System.Drawing.Point(32, 92);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(42, 16);
this.label3.TabIndex = 5;
this.label3.Text = "Login :";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// DBLoginTxt
//
this.DBLoginTxt.Location = new System.Drawing.Point(78, 90);
this.DBLoginTxt.Name = "DBLoginTxt";
this.DBLoginTxt.Size = new System.Drawing.Size(142, 20);
this.DBLoginTxt.TabIndex = 6;
this.DBLoginTxt.Text = "";
//
// label4
//
this.label4.BackColor = System.Drawing.SystemColors.Control;
this.label4.Location = new System.Drawing.Point(14, 118);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(60, 16);
this.label4.TabIndex = 7;
this.label4.Text = "Password :";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// DBPwdTxt
//
this.DBPwdTxt.Location = new System.Drawing.Point(78, 116);
this.DBPwdTxt.Name = "DBPwdTxt";
this.DBPwdTxt.PasswordChar = '*';
this.DBPwdTxt.Size = new System.Drawing.Size(142, 20);
this.DBPwdTxt.TabIndex = 8;
this.DBPwdTxt.Text = "";
//
// ClearAllBtn
//
this.ClearAllBtn.Cursor = System.Windows.Forms.Cursors.Hand;
this.ClearAllBtn.Location = new System.Drawing.Point(230, 276);
this.ClearAllBtn.Name = "ClearAllBtn";
this.ClearAllBtn.Size = new System.Drawing.Size(114, 22);
this.ClearAllBtn.TabIndex = 9;
this.ClearAllBtn.Text = "Clear All";
this.ClearAllBtn.Click += new System.EventHandler(this.OnClearAllDatabases);
//
// CloseBtn
//
this.CloseBtn.BackColor = System.Drawing.SystemColors.Control;
this.CloseBtn.Cursor = System.Windows.Forms.Cursors.Hand;
this.CloseBtn.Location = new System.Drawing.Point(274, 440);
this.CloseBtn.Name = "CloseBtn";
this.CloseBtn.Size = new System.Drawing.Size(114, 24);
this.CloseBtn.TabIndex = 10;
this.CloseBtn.Text = "Cancel";
this.CloseBtn.Click += new System.EventHandler(this.OnExit);
//
// ConnectBtn
//
this.ConnectBtn.Cursor = System.Windows.Forms.Cursors.Hand;
this.ConnectBtn.Location = new System.Drawing.Point(276, 100);
this.ConnectBtn.Name = "ConnectBtn";
this.ConnectBtn.Size = new System.Drawing.Size(110, 22);
this.ConnectBtn.TabIndex = 11;
this.ConnectBtn.Text = "Connect";
this.ConnectBtn.Click += new System.EventHandler(this.OnConnect);
//
// ServerList
//
this.ServerList.Location = new System.Drawing.Point(78, 66);
this.ServerList.Name = "ServerList";
this.ServerList.Size = new System.Drawing.Size(194, 21);
this.ServerList.TabIndex = 12;
//
// GetServersBtn
//
this.GetServersBtn.Cursor = System.Windows.Forms.Cursors.Hand;
this.GetServersBtn.Location = new System.Drawing.Point(276, 66);
this.GetServersBtn.Name = "GetServersBtn";
this.GetServersBtn.Size = new System.Drawing.Size(114, 22);
this.GetServersBtn.TabIndex = 13;
this.GetServersBtn.Text = "Get Servers List";
this.GetServersBtn.Click += new System.EventHandler(this.OnGetServers);
//
// StatusText
//
this.StatusText.BackColor = System.Drawing.SystemColors.Control;
this.StatusText.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.StatusText.Dock = System.Windows.Forms.DockStyle.Bottom;
this.StatusText.Location = new System.Drawing.Point(0, 473);
this.StatusText.Name = "StatusText";
this.StatusText.ReadOnly = true;
this.StatusText.Size = new System.Drawing.Size(416, 20);
this.StatusText.TabIndex = 14;
this.StatusText.Text = "Ready";
//
// label6
//
this.label6.BackColor = System.Drawing.SystemColors.Control;
this.label6.Location = new System.Drawing.Point(22, 334);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(66, 16);
this.label6.TabIndex = 16;
this.label6.Text = "Ini file path :";
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// VSSIniPath
//
this.VSSIniPath.Location = new System.Drawing.Point(90, 332);
this.VSSIniPath.Name = "VSSIniPath";
this.VSSIniPath.Size = new System.Drawing.Size(226, 20);
this.VSSIniPath.TabIndex = 17;
this.VSSIniPath.Text = "";
//
// button1
//
this.button1.Cursor = System.Windows.Forms.Cursors.Hand;
this.button1.Location = new System.Drawing.Point(320, 332);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(78, 22);
this.button1.TabIndex = 18;
this.button1.Text = "Browse ...";
this.button1.Click += new System.EventHandler(this.OnVssBrowse);
//
// label7
//
this.label7.BackColor = System.Drawing.SystemColors.Control;
this.label7.Location = new System.Drawing.Point(42, 356);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(44, 16);
this.label7.TabIndex = 19;
this.label7.Text = "Login :";
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// VSSLoginTxt
//
this.VSSLoginTxt.Location = new System.Drawing.Point(90, 354);
this.VSSLoginTxt.Name = "VSSLoginTxt";
this.VSSLoginTxt.Size = new System.Drawing.Size(126, 20);
this.VSSLoginTxt.TabIndex = 20;
this.VSSLoginTxt.Text = "";
//
// label8
//
this.label8.BackColor = System.Drawing.SystemColors.Control;
this.label8.Location = new System.Drawing.Point(30, 380);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(60, 16);
this.label8.TabIndex = 21;
this.label8.Text = "Password :";
this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// VSSPwdTxt
//
this.VSSPwdTxt.Location = new System.Drawing.Point(90, 378);
this.VSSPwdTxt.Name = "VSSPwdTxt";
this.VSSPwdTxt.PasswordChar = '*';
this.VSSPwdTxt.Size = new System.Drawing.Size(126, 20);
this.VSSPwdTxt.TabIndex = 22;
this.VSSPwdTxt.Text = "";
//
// label9
//
this.label9.BackColor = System.Drawing.SystemColors.Control;
this.label9.Location = new System.Drawing.Point(14, 404);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(76, 16);
this.label9.TabIndex = 23;
this.label9.Text = "Project path :";
this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// VSSProjTxt
//
this.VSSProjTxt.Location = new System.Drawing.Point(90, 402);
this.VSSProjTxt.Name = "VSSProjTxt";
this.VSSProjTxt.Size = new System.Drawing.Size(224, 20);
this.VSSProjTxt.TabIndex = 24;
this.VSSProjTxt.Text = "";
//
// DBGroupBox
//
this.DBGroupBox.BackColor = System.Drawing.SystemColors.Control;
this.DBGroupBox.Location = new System.Drawing.Point(8, 46);
this.DBGroupBox.Name = "DBGroupBox";
this.DBGroupBox.Size = new System.Drawing.Size(402, 260);
this.DBGroupBox.TabIndex = 25;
this.DBGroupBox.TabStop = false;
this.DBGroupBox.Text = "Database ";
//
// groupBox1
//
this.groupBox1.BackColor = System.Drawing.SystemColors.Control;
this.groupBox1.Location = new System.Drawing.Point(10, 312);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(402, 120);
this.groupBox1.TabIndex = 26;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Visual Source Safe";
//
// ScriptDBBtn
//
this.ScriptDBBtn.BackColor = System.Drawing.SystemColors.ControlLight;
this.ScriptDBBtn.Cursor = System.Windows.Forms.Cursors.Hand;
this.ScriptDBBtn.Enabled = false;
this.ScriptDBBtn.Location = new System.Drawing.Point(30, 440);
this.ScriptDBBtn.Name = "ScriptDBBtn";
this.ScriptDBBtn.Size = new System.Drawing.Size(218, 24);
this.ScriptDBBtn.TabIndex = 27;
this.ScriptDBBtn.Text = "Script and Checkin Selected Databases";
this.ScriptDBBtn.Click += new System.EventHandler(this.OnScriptDatabase);
//
// CopyRight
//
this.CopyRight.BackColor = System.Drawing.Color.Black;
this.CopyRight.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.CopyRight.Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.CopyRight.ForeColor = System.Drawing.SystemColors.ControlLightLight;
this.CopyRight.Location = new System.Drawing.Point(6, 6);
this.CopyRight.Multiline = true;
this.CopyRight.Name = "CopyRight";
this.CopyRight.ReadOnly = true;
this.CopyRight.Size = new System.Drawing.Size(402, 36);
this.CopyRight.TabIndex = 28;
this.CopyRight.Text = "DB Script Safe for SQL Server\r\nby Sriram Chitturi � 2004";
this.CopyRight.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// DBScriptSafe
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(416, 493);
this.Controls.Add(this.CopyRight);
this.Controls.Add(this.ScriptDBBtn);
this.Controls.Add(this.VSSProjTxt);
this.Controls.Add(this.VSSPwdTxt);
this.Controls.Add(this.VSSLoginTxt);
this.Controls.Add(this.VSSIniPath);
this.Controls.Add(this.DBPwdTxt);
this.Controls.Add(this.DBLoginTxt);
this.Controls.Add(this.StatusText);
this.Controls.Add(this.label9);
this.Controls.Add(this.label8);
this.Controls.Add(this.label7);
this.Controls.Add(this.button1);
this.Controls.Add(this.label6);
this.Controls.Add(this.GetServersBtn);
this.Controls.Add(this.ServerList);
this.Controls.Add(this.ConnectBtn);
this.Controls.Add(this.CloseBtn);
this.Controls.Add(this.ClearAllBtn);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.SelectAllBtn);
this.Controls.Add(this.DBListView);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.DBGroupBox);
this.Controls.Add(this.groupBox1);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "DBScriptSafe";
this.Text = " DB Script Safe";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new DBScriptSafe());
}
private void OnConnect(object sender, System.EventArgs e)
{
try
{
m_sqlServer = new SQLServerClass();
DBLoginTxt.Text.Trim();
if (DBLoginTxt.Text.Length == 0)
m_sqlServer.LoginSecure = true;
m_sqlServer.Connect(ServerList.Text, DBLoginTxt.Text, DBPwdTxt.Text);
DBListView.Items.Clear();
foreach (SQLDMO.Database db in m_sqlServer.Databases)
if (! db.SystemObject)
DBListView.Items.Add(db.Name);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
m_sqlServer = null;
ex = null;
ScriptDBBtn.Enabled = false;
return;
}
ScriptDBBtn.Enabled = true;
}
private void GetServersThread()
{
StatusText.Text = "Getting list of servers ....";
ServerList.Items.Clear();
SQLDMO.ApplicationClass app = new ApplicationClass();
NameList nmList = app.ListAvailableSQLServers();
for (int i=1; i<=nmList.Count; i++)
ServerList.Items.Add(nmList.Item(i));
nmList = null;
app = null;
StatusText.Text = "Ready";
}
private void OnGetServers(object sender, System.EventArgs e)
{
Thread t = new Thread(new ThreadStart(GetServersThread));
t.Start();
}
private void OnExit(object sender, System.EventArgs e)
{
ServerList.Items.Clear();
DBListView.Items.Clear();
Close();
}
private void OnSelectAllDatabases(object sender, System.EventArgs e)
{
foreach (ListViewItem lvi in DBListView.Items)
lvi.Checked = true;
}
private void OnClearAllDatabases(object sender, System.EventArgs e)
{
foreach (ListViewItem lvi in DBListView.Items)
lvi.Checked = false;
}
private void OnVssBrowse(object sender, System.EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.RestoreDirectory = true;
fdlg.DefaultExt = "ini";
fdlg.Filter = "INI files (*.ini)|*.ini";
fdlg.Multiselect = false;
fdlg.Title = "Visual Source safe INI file";
if (fdlg.ShowDialog() == DialogResult.OK)
VSSIniPath.Text = fdlg.FileName;
}
private void OnScriptDatabase(object sender, System.EventArgs e)
{
m_dbList.Clear();
if (m_scriptEngine != null)
if (m_scriptEngineThread != null)
{
if (m_scriptEngineThread.ThreadState == ThreadState.Running)
m_scriptEngineThread.Abort();
m_scriptEngineThread = null;
m_scriptEngine = null;
m_vssDB = null;
ScriptDBBtn.Text = "Script and Checkin Selected Databases";
return;
}
foreach (ListViewItem lvi in DBListView.Items)
if (lvi.Checked) // selected database
m_dbList.Add(lvi.Text);
try
{
m_vssDB = new VSSDatabaseClass();
m_vssDB.Open(VSSIniPath.Text, VSSLoginTxt.Text, VSSPwdTxt.Text);
m_scriptEngine = new ScriptEngine(m_sqlServer,
m_vssDB,
VSSProjTxt.Text);
m_scriptEngine.ScriptDone += new ScriptingHandler(ScriptAndCheckIn);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
ScriptDBBtn.Text = "Stop Scripting";
ScriptAndCheckIn();
}
public void ScriptAndCheckIn()
{
m_scriptEngineThread = null;
if (m_dbList.Count > 0)
{
m_scriptEngine.DatabaseName = (string)m_dbList[0];
m_dbList.RemoveAt(0);
m_scriptEngineThread = new Thread(new ThreadStart(m_scriptEngine.Script));
m_scriptEngineThread.Start();
System.Windows.Forms.Application.DoEvents();
}
else
{
m_vssDB = null;
m_scriptEngine = null;
ScriptDBBtn.Text = "Script and Checkin Selected Databases";
}
}
private void s_transfer_StatusMessage(string Message)
{
StatusText.Text = Message;
System.Windows.Forms.Application.DoEvents();
}
private void s_transfer_ScriptTransferPercentComplete(string Message, int Percent)
{
// progressStatusBar.Value = Percent;
System.Windows.Forms.Application.DoEvents();
}
}
}