Click here to Skip to main content
15,895,746 members
Articles / Programming Languages / SQL

SQL Statement Generator

Rate me:
Please Sign up or sign in to vote.
4.78/5 (45 votes)
14 Dec 20063 min read 257.8K   6.8K   148  
A utility that generates INSERT, UPDATE, or DELETE SQL statements.
using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;


namespace SqlStatementGenerator
{    

    class FileUtilities
    {       

        /// <summary>
        /// Returns a guaranteed unique filename in the temp directory
        /// </summary>
        /// <param name="sExtension"></param>
        /// <returns></returns>
        public static string GetUniqueTempFileName(string sExtension)
        {
            string sUniqueName = System.Guid.NewGuid().ToString() + sExtension;

            return Path.Combine(Path.GetTempPath(), sUniqueName);
        }

        public static string GetUniqueTempFileName(string sPath, string sExtension)
        {
            string sUniqueName = System.Guid.NewGuid().ToString() + sExtension;

            return Path.Combine(sPath, sUniqueName);
        }

        /// <summary>
        /// Returns a guaranteed unique path in the temp directory
        /// </summary>
        /// <param name="sExtension"></param>
        /// <returns></returns>
        public static string GetUniqueTempPath()
        {
            string sUniqueName = System.Guid.NewGuid().ToString();

            return Path.Combine(Path.GetTempPath(), sUniqueName);
        }

        public static string GetUniqueTempPath(string sPath)
        {
            string sUniqueName = System.Guid.NewGuid().ToString();

            return Path.Combine(sPath, sUniqueName);
        }

        /// <summary>
        /// Reads and returns the contents of a file into a string
        /// </summary>
        /// <param name="sFilePath"></param>
        /// <returns></returns>
        public static string ReadFileContents(string sFilePath)
        {
            FileStream file = null;
            StreamReader sr = null;
            string sContents = string.Empty;

            try
            {
                // make sure we're allowed to overwrite the file if it exists
                if (File.Exists(sFilePath) == false)
                {
                    throw new Exception("Cannot read the file '" + sFilePath + "' because it does not exist!");
                }

                // Specify file, instructions, and priveledges
                file = new FileStream(sFilePath, FileMode.OpenOrCreate, FileAccess.Read);

                // Create a new stream to read from a file
                sr = new StreamReader(file);

                // Read contents of file into a string
                sContents = sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                throw new Exception("ReadFileContents() failed with error: " + ex.Message);
            }
            finally
            {
                // Close StreamReader
                if (sr != null)
                    sr.Close();

                // Close file
                if (file != null)
                    file.Close();
            }

            return sContents;
        }

        /// <summary>
        /// Writes a string/contents to a given filepath
        /// </summary>
        /// <param name="sFilePath"></param>
        /// <param name="sContents"></param>
        /// <param name="bOverwrite"></param>
        public static void WriteFileContents(string sFilePath, string sContents, bool bOverwrite)
        {
            FileStream file = null;
            StreamWriter sw = null;

            try
            {
                // make sure we're allowed to overwrite the file if it exists
                if (bOverwrite == false)
                {
                    if (File.Exists(sFilePath) == true)
                    {
                        throw new Exception("Cannot write the file '" + sFilePath + "' because it already exists!");
                    }
                }

                // Specify file, instructions, and privelegdes
                file = new FileStream(sFilePath, FileMode.Create, FileAccess.Write);

                // Create a new stream to write to the file
                sw = new StreamWriter(file);

                // Write a string to the file
                sw.Write(sContents);
            }
            catch (Exception ex)
            {
                throw new Exception("WriteFileContents() failed with error: " + ex.Message);
            }
            finally
            {
                // Close StreamWriter
                if (sw != null)
                    sw.Close();

                // Close file
                if (file != null)
                    file.Close();
            }
        }


    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
United States United States
SOFTWARE: Chris Hambleton is a Software Developer with proven experience in developing both web and Windows client-server applications with WPF, ASP.NET, C#, SQL Server, VB.NET, Visual C++, and VB6.

Chris's website is at ChrisHambleton.com and he has a small web-hosting/consulting business called CustomersInFocus.com. He has several other websites such as EzekielWatch.com, iWriterPro.com, and BookBlitzer.com.

WRITING: He has also written several fiction books ("The Time of Jacob's Trouble" and "Endeavor in Time"), available at CWHambleton.com and of course, at Amazon.com (Full Amazon Profile).

Comments and Discussions