Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / SQL

SQL Editor for Database Developers

Rate me:
Please Sign up or sign in to vote.
4.55/5 (65 votes)
10 Mar 2010GPL317 min read 251.5K   9K   236  
SQL editor with syntax parser, direct editing, code execution, database backup, table comparison, script generation, time measurement
// -------------------------------------------------------
// SqlBuilder by Elm�Soft
// www.netcult.ch/elmue
// www.codeproject.com/KB/database/SqlBuilder.aspx
// -------------------------------------------------------

using System;
using System.Runtime.InteropServices;

namespace SqlBuilder
{
	/// <summary>
	/// The performance counter is better than 1 ms while the Tickcount only increments approx every 15 ms
	/// If the hardware supportes it, this class uses the more exact performance counter.
	/// .NET does not support a performance counter. DateTime.Tick is not precise!
	/// </summary>
	public class Measure
	{
		[DllImport("kernel32.dll", EntryPoint="QueryPerformanceCounter")]
		private static extern int QueryPerformanceCounter(out long s64_Counter);

		[DllImport("kernel32.dll", EntryPoint="QueryPerformanceFrequency")]
		private static extern int QueryPerformanceFrequency(out long s64_Frequency);

		private static long s64_Frequency = 0;
		private long        s64_Start;

		public Measure()
		{
			if (s64_Frequency == 0)
			{
				QueryPerformanceFrequency(out s64_Frequency);
				s64_Frequency /= 1000;
			}
			Start();
		}

		/// <summary>
		/// Start a new measurement
		/// </summary>
		public void Start()
		{
			if (s64_Frequency == 0)
				s64_Start = Environment.TickCount;
			else
				QueryPerformanceCounter(out s64_Start);
		}

		/// <summary>
		/// returns the time in ms which has elapsed since creating this class or calling Start()
		/// </summary>
		public int ElapsedTimeInt
		{
			get
			{
				if (s64_Frequency == 0) // hardware does not support Performance counters
				{
					return Environment.TickCount - (int)s64_Start;
				}
				else
				{
					long s64_Now;
					QueryPerformanceCounter(out s64_Now);
					return (int) ((s64_Now - s64_Start) / s64_Frequency);
				}
			}
		}

		public string ElapsedTimeStr
		{
			get
			{
				int s32_Elapsed = ElapsedTimeInt;
				return string.Format("{0}.{1}", s32_Elapsed/1000, (s32_Elapsed%1000).ToString("000"));
			}
		}
	}
}

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 GNU General Public License (GPLv3)


Written By
Software Developer (Senior) ElmüSoft
Chile Chile
Software Engineer since 40 years.

Comments and Discussions