Click here to Skip to main content
15,891,529 members
Articles / Desktop Programming / MFC

QASharp V1.3 [Query Analyzer like tool for MSDE database]

Rate me:
Please Sign up or sign in to vote.
4.67/5 (16 votes)
11 Oct 2004CPOL1 min read 125.8K   2.6K   53  
This project is meant to provide a tool similar to query analyzer which can be used to execute queries, create tables, and do all other common database activities.
  • qasharpv13.zip
    • QASharpV1.3
      • app.config
      • AssemblyInfo.cs
      • bin
        • images
          • _Progress.gif
          • _splash_Screen.gif
          • checkbox.gif
          • close.gif
          • close_over.gif
          • drop.gif
          • Icon_SqlServer.gif
          • iconCritical.gif
          • IconInformation.gif
          • IconOpenFile.gif
          • Icons_Database.gif
          • Icons_Defaults.gif
          • Icons_Diagrams.gif
          • Icons_FullTextCatalogs.gif
          • Icons_Roles.gif
          • Icons_Rules.gif
          • Icons_StoredProcudures.gif
          • Icons_Tables.gif
          • Icons_UserDefinedDataTypes.gif
          • Icons_UserDefinedFunctions.gif
          • Icons_Users.gif
          • Icons_Views.gif
          • iconSecurity.gif
          • IconWarning.gif
          • key.gif
          • Large_Icons_Databases.gif
          • large_Icons_Defaults.gif
          • large_Icons_Login.gif
          • large_Icons_Query.gif
          • Large_Icons_ServerRoles.gif
          • Large_Icons_StoredProcedure.gif
          • Large_Icons_TableProps.gif
          • large_Icons_Users.gif
          • large_Icons_Views.gif
          • navBar_Background.gif
          • navBar_button_Back.gif
          • navBar_button_Back_over.gif
          • navBar_button_Forward.gif
          • navBar_button_Forward_over.gif
          • navbar_button_Insert.gif
          • navBar_button_Logout.gif
          • navBar_button_Logout_over.gif
          • navbar_button_NewDB.gif
          • navbar_button_NewDB_over.gif
          • navBar_button_NewLogin.gif
          • navBar_button_NewLogin_over.gif
          • navbar_button_Properties.gif
          • navbar_button_Properties_ov.gif
          • navBar_button_Query.gif
          • navBar_button_Query_over.gif
          • navBar_button_Refresh.gif
          • navBar_button_Refresh_over.gif
          • navBar_button_ToDo.gif
          • navBar_button_ToDo_over.gif
          • Process_background.gif
          • Process_runnable.gif
          • Process_sleeping.gif
          • right.ico
          • small_Icons_Defaults.gif
          • small_Icons_Diagrams.gif
          • small_Icons_Folder.gif
          • small_Icons_Full_Text_Catal.gif
          • small_Icons_Logins.gif
          • small_Icons_ProcessInfo.gif
          • small_Icons_Roles.gif
          • small_Icons_Rules.gif
          • small_Icons_ServerRoles.gif
          • small_Icons_Stored_Procedur.gif
          • small_Icons_Tables.gif
          • small_Icons_User_Defined_Fu.gif
          • small_Icons_User_Defined_Ty.gif
          • small_Icons_Users.gif
          • small_Icons_Views.gif
          • smallIcon_Database.gif
          • smallIcon_Table.gif
          • spacer.gif
          • system_icon.gif
          • TellAFriend.gif
          • windows
            • Border_Bottom.gif
            • Border_BottomLeft.gif
            • Border_BottomRight.gif
            • Border_Left.gif
            • Border_Right.gif
            • icon_ASPEntMan.gif
            • TitleBar_Background.gif
            • TitleBar_CloseButton.gif
            • TitleBar_CloseButtonOver.gif
            • TitleBar_Left.gif
            • TitleBar_Right.gif
        • Release
      • Common
      • DBControls
      • DBLayer
      • frmConnect.cs
      • frmConnect.resx
      • frmPrintPreview.cs
      • frmPrintPreview.resx
      • frmQASharp.cs
      • frmQASharp.resx
      • frmQueryWriter.cs
      • frmQueryWriter.resx
      • frmTestControl.cs
      • frmTestControl.resx
      • Images
        • App.ico
        • Icon_SqlServer.gif
        • Icons_StoredProcudures.gif
        • Icons_Tables.gif
        • Icons_Views.gif
        • large_Icons_Views.gif
        • smallIcon_Database.gif
        • smallIcon_Table.gif
      • Persist.cs
      • QASharp.csproj
      • QASharp.csproj.user
      • QASharp.csproj.vspscc
      • QASharp.sln
  • qasharp12_src.zip
    • QASharp121.jpg
    • QASharp122.jpg
    • QASharpV1.2
      • app.config
      • AssemblyInfo.cs
      • bin
        • images
          • _Progress.gif
          • _splash_Screen.gif
          • checkbox.gif
          • close.gif
          • close_over.gif
          • drop.gif
          • Icon_SqlServer.gif
          • iconCritical.gif
          • IconInformation.gif
          • IconOpenFile.gif
          • Icons_Database.gif
          • Icons_Defaults.gif
          • Icons_Diagrams.gif
          • Icons_FullTextCatalogs.gif
          • Icons_Roles.gif
          • Icons_Rules.gif
          • Icons_StoredProcudures.gif
          • Icons_Tables.gif
          • Icons_UserDefinedDataTypes.gif
          • Icons_UserDefinedFunctions.gif
          • Icons_Users.gif
          • Icons_Views.gif
          • iconSecurity.gif
          • IconWarning.gif
          • key.gif
          • Large_Icons_Databases.gif
          • large_Icons_Defaults.gif
          • large_Icons_Login.gif
          • large_Icons_Query.gif
          • Large_Icons_ServerRoles.gif
          • Large_Icons_StoredProcedure.gif
          • Large_Icons_TableProps.gif
          • large_Icons_Users.gif
          • large_Icons_Views.gif
          • navBar_Background.gif
          • navBar_button_Back.gif
          • navBar_button_Back_over.gif
          • navBar_button_Forward.gif
          • navBar_button_Forward_over.gif
          • navbar_button_Insert.gif
          • navBar_button_Logout.gif
          • navBar_button_Logout_over.gif
          • navbar_button_NewDB.gif
          • navbar_button_NewDB_over.gif
          • navBar_button_NewLogin.gif
          • navBar_button_NewLogin_over.gif
          • navbar_button_Properties.gif
          • navbar_button_Properties_ov.gif
          • navBar_button_Query.gif
          • navBar_button_Query_over.gif
          • navBar_button_Refresh.gif
          • navBar_button_Refresh_over.gif
          • navBar_button_ToDo.gif
          • navBar_button_ToDo_over.gif
          • Process_background.gif
          • Process_runnable.gif
          • Process_sleeping.gif
          • right.ico
          • small_Icons_Defaults.gif
          • small_Icons_Diagrams.gif
          • small_Icons_Folder.gif
          • small_Icons_Full_Text_Catal.gif
          • small_Icons_Logins.gif
          • small_Icons_ProcessInfo.gif
          • small_Icons_Roles.gif
          • small_Icons_Rules.gif
          • small_Icons_ServerRoles.gif
          • small_Icons_Stored_Procedur.gif
          • small_Icons_Tables.gif
          • small_Icons_User_Defined_Fu.gif
          • small_Icons_User_Defined_Ty.gif
          • small_Icons_Users.gif
          • small_Icons_Views.gif
          • smallIcon_Database.gif
          • smallIcon_Table.gif
          • spacer.gif
          • system_icon.gif
          • TellAFriend.gif
          • windows
            • Border_Bottom.gif
            • Border_BottomLeft.gif
            • Border_BottomRight.gif
            • Border_Left.gif
            • Border_Right.gif
            • icon_ASPEntMan.gif
            • TitleBar_Background.gif
            • TitleBar_CloseButton.gif
            • TitleBar_CloseButtonOver.gif
            • TitleBar_Left.gif
            • TitleBar_Right.gif
        • Release
      • Common
      • DBControls
      • DBLayer
      • frmConnect.cs
      • frmConnect.resx
      • frmQASharp.cs
      • frmQASharp.resx
      • frmQueryWriter.cs
      • frmQueryWriter.resx
      • frmTestControl.cs
      • frmTestControl.resx
      • Images
        • App.ico
        • Icon_SqlServer.gif
        • Icons_StoredProcudures.gif
        • Icons_Tables.gif
        • Icons_Views.gif
        • large_Icons_Views.gif
        • smallIcon_Database.gif
        • smallIcon_Table.gif
      • obj
        • Release
          • temp
          • TempPE
      • QASharp.csproj
      • QASharp.csproj.user
      • QASharp.csproj.vspscc
      • QASharp.sln
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text.RegularExpressions;
using System.IO;
using DataLayer;

namespace QASharp
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmQASharp : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;


		#region "Form level variables"

		    SqlDBHandler dbh = new SqlDBHandler(@"","master","sa","");
		
			QASharp.DataBase db = new QASharp.DataBase();
			DataSet ds;
			string[] arrQuery;
			string ErrorText;

		#endregion

		private StringReader myReader;

		public System.Windows.Forms.ComboBox cmbDatabase;

		string currentDatabase = "master";
		string fileName		   = "";
		string uid			   = "";
		string pwd			   = "";
		string server		   = "";
		string database		   = "";
		
		private System.Windows.Forms.ImageList imlToolBar;
		private System.Windows.Forms.ToolBarButton tbNew;
		private System.Windows.Forms.ToolBarButton tbOpen;
		private System.Windows.Forms.ToolBarButton tbSave;
		private System.Windows.Forms.ToolBarButton tbCut;
		private System.Windows.Forms.ToolBarButton tbCopy;
		private System.Windows.Forms.ToolBarButton tbPaste;
		private System.Windows.Forms.ToolBarButton tbSep;
		private System.Windows.Forms.ToolBarButton tbExecute;
		private System.Windows.Forms.ToolBar tbMain;
		private System.Windows.Forms.MainMenu mnuMain;
		private System.Windows.Forms.MenuItem mnuWindow;
		private System.Windows.Forms.OpenFileDialog openFD;
		private System.Windows.Forms.SaveFileDialog saveFD;
		private System.Windows.Forms.MenuItem mnuFile;
		private System.Windows.Forms.MenuItem mnuFileConnect;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem mnuFileExit;
		public System.Windows.Forms.StatusBar sbMain;
		private System.Windows.Forms.StatusBarPanel sbStatus;
		private System.Windows.Forms.MenuItem mnuHelp;
		private System.Windows.Forms.MenuItem mnuHelpShowErrors;
		private System.Windows.Forms.Panel pnlServer;
		private System.Windows.Forms.MenuItem mnuTools;
		private System.Windows.Forms.MenuItem mnuToolsObjectBrowser;
		private System.Windows.Forms.MenuItem mnuToolsOBShowHide;
		private System.Windows.Forms.MenuItem mnuFileNew;
		private System.Windows.Forms.MenuItem mnuFileOpen;
		private System.Windows.Forms.MenuItem mnuFileSave;
		public DBControls.DataBaseTree dataBaseTree1;
		private System.Windows.Forms.MenuItem mnuFilePrint;
		private System.Drawing.Printing.PrintDocument pdQuery;
		private System.Windows.Forms.MenuItem mnuFilePrintPreview;
		private System.Windows.Forms.PrintDialog printDialog1;
		private System.Windows.Forms.MenuItem mnuPrintSettings;
		private System.Windows.Forms.MenuItem mnuHelpAbref;
		

		public frmQASharp()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmQASharp));
			this.cmbDatabase = new System.Windows.Forms.ComboBox();
			this.tbMain = new System.Windows.Forms.ToolBar();
			this.tbNew = new System.Windows.Forms.ToolBarButton();
			this.tbOpen = new System.Windows.Forms.ToolBarButton();
			this.tbSave = new System.Windows.Forms.ToolBarButton();
			this.tbCut = new System.Windows.Forms.ToolBarButton();
			this.tbCopy = new System.Windows.Forms.ToolBarButton();
			this.tbPaste = new System.Windows.Forms.ToolBarButton();
			this.tbSep = new System.Windows.Forms.ToolBarButton();
			this.tbExecute = new System.Windows.Forms.ToolBarButton();
			this.imlToolBar = new System.Windows.Forms.ImageList(this.components);
			this.mnuMain = new System.Windows.Forms.MainMenu();
			this.mnuFile = new System.Windows.Forms.MenuItem();
			this.mnuFileNew = new System.Windows.Forms.MenuItem();
			this.mnuFileOpen = new System.Windows.Forms.MenuItem();
			this.mnuFileSave = new System.Windows.Forms.MenuItem();
			this.mnuFilePrintPreview = new System.Windows.Forms.MenuItem();
			this.mnuFilePrint = new System.Windows.Forms.MenuItem();
			this.mnuFileConnect = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.mnuFileExit = new System.Windows.Forms.MenuItem();
			this.mnuTools = new System.Windows.Forms.MenuItem();
			this.mnuToolsObjectBrowser = new System.Windows.Forms.MenuItem();
			this.mnuToolsOBShowHide = new System.Windows.Forms.MenuItem();
			this.mnuWindow = new System.Windows.Forms.MenuItem();
			this.mnuHelp = new System.Windows.Forms.MenuItem();
			this.mnuHelpShowErrors = new System.Windows.Forms.MenuItem();
			this.mnuHelpAbref = new System.Windows.Forms.MenuItem();
			this.openFD = new System.Windows.Forms.OpenFileDialog();
			this.saveFD = new System.Windows.Forms.SaveFileDialog();
			this.sbMain = new System.Windows.Forms.StatusBar();
			this.sbStatus = new System.Windows.Forms.StatusBarPanel();
			this.pnlServer = new System.Windows.Forms.Panel();
			this.dataBaseTree1 = new DBControls.DataBaseTree();
			this.pdQuery = new System.Drawing.Printing.PrintDocument();
			this.printDialog1 = new System.Windows.Forms.PrintDialog();
			this.mnuPrintSettings = new System.Windows.Forms.MenuItem();
			((System.ComponentModel.ISupportInitialize)(this.sbStatus)).BeginInit();
			this.pnlServer.SuspendLayout();
			this.SuspendLayout();
			// 
			// cmbDatabase
			// 
			this.cmbDatabase.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbDatabase.Location = new System.Drawing.Point(296, 3);
			this.cmbDatabase.Name = "cmbDatabase";
			this.cmbDatabase.Size = new System.Drawing.Size(160, 21);
			this.cmbDatabase.TabIndex = 7;
			this.cmbDatabase.SelectedIndexChanged += new System.EventHandler(this.cmbDatabase_SelectedIndexChanged);
			// 
			// tbMain
			// 
			this.tbMain.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
			this.tbMain.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																					  this.tbNew,
																					  this.tbOpen,
																					  this.tbSave,
																					  this.tbCut,
																					  this.tbCopy,
																					  this.tbPaste,
																					  this.tbSep,
																					  this.tbExecute});
			this.tbMain.DropDownArrows = true;
			this.tbMain.ImageList = this.imlToolBar;
			this.tbMain.Name = "tbMain";
			this.tbMain.ShowToolTips = true;
			this.tbMain.Size = new System.Drawing.Size(784, 25);
			this.tbMain.TabIndex = 9;
			this.tbMain.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
			// 
			// tbNew
			// 
			this.tbNew.ImageIndex = 0;
			this.tbNew.Tag = "New";
			// 
			// tbOpen
			// 
			this.tbOpen.ImageIndex = 1;
			this.tbOpen.Tag = "Open";
			// 
			// tbSave
			// 
			this.tbSave.ImageIndex = 2;
			this.tbSave.Tag = "Save";
			// 
			// tbCut
			// 
			this.tbCut.ImageIndex = 3;
			this.tbCut.Tag = "Cut";
			this.tbCut.Visible = false;
			// 
			// tbCopy
			// 
			this.tbCopy.ImageIndex = 4;
			this.tbCopy.Tag = "v";
			this.tbCopy.Visible = false;
			// 
			// tbPaste
			// 
			this.tbPaste.ImageIndex = 5;
			this.tbPaste.Tag = "Paste";
			this.tbPaste.Visible = false;
			// 
			// tbSep
			// 
			this.tbSep.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// tbExecute
			// 
			this.tbExecute.ImageIndex = 6;
			this.tbExecute.Tag = "Execute";
			this.tbExecute.ToolTipText = "Press \"F5\" to execute query";
			// 
			// imlToolBar
			// 
			this.imlToolBar.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
			this.imlToolBar.ImageSize = new System.Drawing.Size(16, 16);
			this.imlToolBar.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imlToolBar.ImageStream")));
			this.imlToolBar.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// mnuMain
			// 
			this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					this.mnuFile,
																					this.mnuTools,
																					this.mnuWindow,
																					this.mnuHelp});
			// 
			// mnuFile
			// 
			this.mnuFile.Index = 0;
			this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					this.mnuFileNew,
																					this.mnuFileOpen,
																					this.mnuFileSave,
																					this.mnuPrintSettings,
																					this.mnuFilePrintPreview,
																					this.mnuFilePrint,
																					this.mnuFileConnect,
																					this.menuItem1,
																					this.menuItem2,
																					this.mnuFileExit});
			this.mnuFile.Text = "File";
			// 
			// mnuFileNew
			// 
			this.mnuFileNew.Index = 0;
			this.mnuFileNew.Text = "&New";
			this.mnuFileNew.Click += new System.EventHandler(this.mnuFileNew_Click);
			// 
			// mnuFileOpen
			// 
			this.mnuFileOpen.Index = 1;
			this.mnuFileOpen.Text = "&Open...";
			this.mnuFileOpen.Click += new System.EventHandler(this.mnuFileOpen_Click);
			// 
			// mnuFileSave
			// 
			this.mnuFileSave.Index = 2;
			this.mnuFileSave.Text = "&Save";
			this.mnuFileSave.Click += new System.EventHandler(this.mnuFileSave_Click);
			// 
			// mnuFilePrintPreview
			// 
			this.mnuFilePrintPreview.Index = 4;
			this.mnuFilePrintPreview.Text = "Print Pre&view";
			this.mnuFilePrintPreview.Click += new System.EventHandler(this.mnuFilePrintPreview_Click);
			// 
			// mnuFilePrint
			// 
			this.mnuFilePrint.Index = 5;
			this.mnuFilePrint.Text = "&Print...";
			this.mnuFilePrint.Click += new System.EventHandler(this.mnuFilePrint_Click);
			// 
			// mnuFileConnect
			// 
			this.mnuFileConnect.Index = 6;
			this.mnuFileConnect.Text = "&Connect...";
			this.mnuFileConnect.Click += new System.EventHandler(this.mnuFileConnect_Click);
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 7;
			this.menuItem1.Text = "&Disconnect";
			this.menuItem1.Visible = false;
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 8;
			this.menuItem2.Text = "-";
			// 
			// mnuFileExit
			// 
			this.mnuFileExit.Index = 9;
			this.mnuFileExit.Text = "E&xit";
			this.mnuFileExit.Click += new System.EventHandler(this.mnuFileExit_Click);
			// 
			// mnuTools
			// 
			this.mnuTools.Index = 1;
			this.mnuTools.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.mnuToolsObjectBrowser});
			this.mnuTools.Text = "&Tools";
			// 
			// mnuToolsObjectBrowser
			// 
			this.mnuToolsObjectBrowser.Index = 0;
			this.mnuToolsObjectBrowser.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																								  this.mnuToolsOBShowHide});
			this.mnuToolsObjectBrowser.Text = "Object &Browser";
			// 
			// mnuToolsOBShowHide
			// 
			this.mnuToolsOBShowHide.Checked = true;
			this.mnuToolsOBShowHide.Index = 0;
			this.mnuToolsOBShowHide.Shortcut = System.Windows.Forms.Shortcut.F8;
			this.mnuToolsOBShowHide.Text = "&Show/Hide";
			this.mnuToolsOBShowHide.Click += new System.EventHandler(this.mnuToolsOBShowHide_Click);
			// 
			// mnuWindow
			// 
			this.mnuWindow.Index = 2;
			this.mnuWindow.MdiList = true;
			this.mnuWindow.Text = "Window";
			// 
			// mnuHelp
			// 
			this.mnuHelp.Index = 3;
			this.mnuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					this.mnuHelpShowErrors,
																					this.mnuHelpAbref});
			this.mnuHelp.Text = "&Help";
			// 
			// mnuHelpShowErrors
			// 
			this.mnuHelpShowErrors.Index = 0;
			this.mnuHelpShowErrors.Text = "&Show Errors";
			this.mnuHelpShowErrors.Click += new System.EventHandler(this.mnuHelpShowErrors_Click);
			// 
			// mnuHelpAbref
			// 
			this.mnuHelpAbref.Index = 1;
			this.mnuHelpAbref.Text = "&About...";
			this.mnuHelpAbref.Click += new System.EventHandler(this.mnuHelpAbref_Click);
			// 
			// saveFD
			// 
			this.saveFD.FileName = "doc1";
			// 
			// sbMain
			// 
			this.sbMain.Location = new System.Drawing.Point(0, 423);
			this.sbMain.Name = "sbMain";
			this.sbMain.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																					  this.sbStatus});
			this.sbMain.ShowPanels = true;
			this.sbMain.Size = new System.Drawing.Size(784, 22);
			this.sbMain.TabIndex = 11;
			// 
			// sbStatus
			// 
			this.sbStatus.Width = 180;
			// 
			// pnlServer
			// 
			this.pnlServer.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.pnlServer.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.dataBaseTree1});
			this.pnlServer.Dock = System.Windows.Forms.DockStyle.Left;
			this.pnlServer.Location = new System.Drawing.Point(0, 25);
			this.pnlServer.Name = "pnlServer";
			this.pnlServer.Size = new System.Drawing.Size(168, 398);
			this.pnlServer.TabIndex = 13;
			// 
			// dataBaseTree1
			// 
			this.dataBaseTree1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.dataBaseTree1.Name = "dataBaseTree1";
			this.dataBaseTree1.Size = new System.Drawing.Size(164, 394);
			this.dataBaseTree1.TabIndex = 0;
			this.dataBaseTree1.OnSPSelected += new System.EventHandler(this.Tree_SPSelected);
			// 
			// pdQuery
			// 
			this.pdQuery.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pdQuery_PrintPage);
			// 
			// mnuPrintSettings
			// 
			this.mnuPrintSettings.Index = 3;
			this.mnuPrintSettings.Text = "Print Se&ttings...";
			this.mnuPrintSettings.Click += new System.EventHandler(this.mnuPrintSettings_Click);
			// 
			// frmQASharp
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(784, 445);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.pnlServer,
																		  this.sbMain,
																		  this.cmbDatabase,
																		  this.tbMain});
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.IsMdiContainer = true;
			this.KeyPreview = true;
			this.Menu = this.mnuMain;
			this.Name = "frmQASharp";
			this.Text = "QA Sharp";
			this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmQASharp_KeyDown);
			this.Load += new System.EventHandler(this.frmQASharp_Load);
			((System.ComponentModel.ISupportInitialize)(this.sbStatus)).EndInit();
			this.pnlServer.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmQASharp());
		}

		private void frmQASharp_Load(object sender, System.EventArgs e)
		{
			DataTable dt;
			dt = db.GetDatabases();
			
			try
			{
				cmbDatabase.DataSource= dt;
				cmbDatabase.DisplayMember = dt.Columns[0].ToString();
				cmbDatabase.ValueMember=dt.Columns[1].ToString();
			
				Common.Common.SetDefaultDatabase(cmbDatabase);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				return;
			}

			try 
			{
				NewDocument();
			}
			catch (Exception ex)
			{
					MessageBox.Show(ex.Message);
			}
			
			//fill the tree with database and table info
			foreach(DataRow dr in dt.Rows)
				dataBaseTree1.FillTree(server,dr[0].ToString() ,uid,pwd,Persist.IsTrusted);
		}
		
		private void cmbDatabase_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			currentDatabase = cmbDatabase.Text;
			
			string [] arrConn;									
			
			arrConn = System.Configuration.ConfigurationSettings.AppSettings.GetValues("uid");
			uid = arrConn[0];
			arrConn = System.Configuration.ConfigurationSettings.AppSettings.GetValues("pwd");
			pwd = arrConn[0];
			arrConn = System.Configuration.ConfigurationSettings.AppSettings.GetValues("server");
			server = arrConn[0];
			arrConn = System.Configuration.ConfigurationSettings.AppSettings.GetValues("database");
			database= arrConn[0];
								
			 
			dbh = new SqlDBHandler(server,currentDatabase,uid,pwd);
		}

		private void Tree_SPSelected(object o , EventArgs e)
		{
			NewDocument(dataBaseTree1.SPText);
		}

		/// Function name 	: 	ExecuteQuery
		/// Parameters		:	none
		/// Return			:	none

		/// Purpose			: 	Executes the query user has typed in the query writer
		/// Calls			:	ExecuteQuery(string databaseName, string Query)
		
		private void ExecuteQuery()
		{
			dbh.ERR_MSG = "";
			ErrorText = "";
		
			((frmQueryWriter)this.ActiveMdiChild).DisposeTabPages();

			((frmQueryWriter)this.ActiveMdiChild).ResetButtonIndex();
			
			if (ds != null) 
			{
				ds.Tables.Clear();
				ds = new DataSet();

				arrQuery = null;
			}

			AddToArray(((frmQueryWriter)this.ActiveMdiChild).rtbQueryPad.Text);
						
			foreach(string s1 in arrQuery)
			{
			
				ExecuteQuery(currentDatabase,s1);
				ErrorText += dbh.ERR_MSG + "\n";
			}

			if (ErrorText.Trim() == "")
			{
				sbMain.Panels[0].Text = "Query batch completed.";
				
			}
			else
			{
				sbMain.Panels[0].Text = "Query batch completed with errors.";
				
			}
			
		}

		/// <summary>
		/// Used to execute queries
		/// </summary>
		/// <param name="dataBaseName">the name of the database</param>
		/// <param name="query">the query string to execute</param>
		private void ExecuteQuery(string dataBaseName , string query)
		{
				
			DataTable dt;
			if (ds == null) 
			{
				ds  = new DataSet("result");
			}
			if (query.Trim() == "")
					return;

			try
			{
				dt = dbh.ExecDT(query);
				if (dt != null)
				{
					ds.Tables.Add(dt);
					((frmQueryWriter)this.ActiveMdiChild).RenderGrid(dt);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			
		}
		
		
		/// Function name 		: 	AddToArray
		/// Parameters			:	(in string QueryText)
		/// Return				:	none
		/// Purpose				: 	Splits the query based on �go� delimeter and 
		///							stores it in array [arrQuery]
		///							This [arrQuery] is used in ExecuteQuery()
		///

		private void AddToArray(string QueryText)
		{
			Regex r = new Regex("\\bgo\\b",RegexOptions.IgnoreCase);	// Thanks to Michael Potter V1.1
			arrQuery = r.Split(QueryText);
        }

		private void ShowErrors()
		{
			if (ErrorText.Trim() == "" ) 
			{
				MessageBox.Show("No Errors.");
			}
			else
			{
				MessageBox.Show(ErrorText);
			}
		}
		
		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			switch (e.Button.Tag.ToString())
			{
				case "New":
					NewDocument();
					break;
				case "Open":
					OpenFile();
					break;
				case "Save":
					SaveFile();
					break;
				case "Execute":
					ExecuteQuery();
					break;
			}
		}
		
		private void SaveFile()
		{
			frmQueryWriter frmCurrentForm;
			frmCurrentForm = (frmQueryWriter)this.ActiveMdiChild;

			if (frmCurrentForm.Tag != null)
			{
				if (frmCurrentForm.Tag.ToString().Trim() != "") 
				{
					frmCurrentForm.rtbQueryPad.SaveFile(frmCurrentForm.Tag.ToString(),System.Windows.Forms.RichTextBoxStreamType.PlainText);
				}
			}
			else 
			{
				saveFD.ShowDialog();
				frmCurrentForm.rtbQueryPad.SaveFile(saveFD.FileName,System.Windows.Forms.RichTextBoxStreamType.PlainText);
				frmCurrentForm.Text = saveFD.FileName;
			}
		}

		private void OpenFile()
		{
			openFD.ShowDialog();

			if (openFD.FileName.Trim() != "")
			{
				fileName = openFD.FileName;
				frmQueryWriter frmQuery;
				frmQuery = new frmQueryWriter();
							
				frmQuery.rtbQueryPad.LoadFile(@fileName,RichTextBoxStreamType.PlainText);
				frmQuery.MdiParent = this;
				frmQuery.Text = fileName;
				frmQuery.Tag = fileName;
				
				frmQuery.Show();
				
			}
			
		}
		private void NewDocument()
		{
			frmQueryWriter frmQuery;
			frmQuery = new frmQueryWriter();
			frmQuery.MdiParent = this;
			frmQuery.Show();
			
			sbMain.Panels[0].Text = "Ready";
		}

		private void NewDocument(string textToDisplay)
		{
			frmQueryWriter frmQuery;
			frmQuery = new frmQueryWriter();
			frmQuery.MdiParent = this;
			frmQuery.rtbQueryPad.Text = textToDisplay;

			frmQuery.Show();
			sbMain.Panels[0].Text = "Ready";
		}


		private void mnuFileExit_Click(object sender, System.EventArgs e)
		{
			System.Environment.Exit(0);
		}
		
		private void frmQASharp_KeyDown(object sender,  System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode == Keys.F5)
			{
				
				ExecuteQuery();
			}
		}

		private void mnuHelpAbref_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show("QASharp V1.3","QASharp");
		}

		private void mnuFileConnect_Click(object sender, System.EventArgs e)
		{
			frmConnect fc = new frmConnect();
			fc.MdiParent = this;
			fc.Show();
		}

		private void mnuHelpShowErrors_Click(object sender, System.EventArgs e)
		{
			ShowErrors();		
		}

		private void mnuToolsOBShowHide_Click(object sender, System.EventArgs e)
		{
			if (mnuToolsOBShowHide.Checked == false)
			{	pnlServer.Visible=true;
				mnuToolsOBShowHide.Checked =true;
			}
			else
			{
				pnlServer.Visible = false;
				mnuToolsOBShowHide.Checked =false;
			}
		}

		private void mnuFileNew_Click(object sender, System.EventArgs e)
		{
			NewDocument();
		}

		private void mnuFileOpen_Click(object sender, System.EventArgs e)
		{
			OpenFile();
		}

		private void mnuFileSave_Click(object sender, System.EventArgs e)
		{
			SaveFile();
		}

		private void dataBaseTree1_Load(object sender, System.EventArgs e)
		{
		}

		private void mnuFilePrint_Click(object sender, System.EventArgs e)
		{
			frmQueryWriter frmCurrentForm;
			frmCurrentForm = (frmQueryWriter)this.ActiveMdiChild;

			printDialog1.Document = pdQuery;
			string strText = frmCurrentForm.rtbQueryPad.Text;
			myReader = new StringReader(strText);
			if (printDialog1.ShowDialog() == DialogResult.OK)
			{
				this.pdQuery.Print();
			}
		}

		protected void pdQuery_PrintPage (object sender, System.Drawing.Printing.PrintPageEventArgs ev)
		{
			float linesPerPage = 0;
			float yPosition = 0;
			int count = 0;
			float leftMargin = ev.MarginBounds.Left;
			float topMargin = ev.MarginBounds.Top;
			string line = null;
			
			frmQueryWriter frmCurrentForm;
			frmCurrentForm = (frmQueryWriter)this.ActiveMdiChild;


			Font printFont = frmCurrentForm.rtbQueryPad.Font;
			SolidBrush myBrush = new SolidBrush(Color.Black);

			// Work out the number of lines per page, using the MarginBounds.
			linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);

			// Iterate over the string using the StringReader, printing each line.
			while(count < linesPerPage && ((line=myReader.ReadLine()) != null)) 
			{
				// calculate the next line position based on 
				// the height of the font according to the printing device
				yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));

				// draw the next line in the rich edit control
				
				ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
				count++;
			}

			// If there are more lines, print another page.
			if(line != null)
				ev.HasMorePages = true;
			else
				ev.HasMorePages = false;

			myBrush.Dispose();
   			
		}

		private void mnuFilePrintPreview_Click(object sender, System.EventArgs e)
		{
			frmQueryWriter frmCurrentForm;
			frmCurrentForm = (frmQueryWriter)this.ActiveMdiChild;

			try
			{
				string strText = frmCurrentForm.rtbQueryPad.Text;
				myReader = new StringReader(strText);
				PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
				printPreviewDialog1.Document = this.pdQuery;
				printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D ;
				printPreviewDialog1.ShowDialog();
			}
			catch(Exception exp)
			{
				System.Console.WriteLine(exp.Message.ToString());
			}		
		}

		private void mnuPrintSettings_Click(object sender, System.EventArgs e)
		{
			this.printDialog1.Document = this.pdQuery;
			printDialog1.ShowDialog();
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Algorisys Technologies Pvt. Ltd.
India India
Co Founder at Algorisys Technologies Pvt. Ltd.

http://algorisys.com/
https://teachyourselfcoding.com/ (free early access)
https://www.youtube.com/user/tekacademylabs/

Comments and Discussions