SQL SYNTAX REVIEWER

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.IO;
namespace SqlSyntaxReviewer
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
         InitializeComponent();
        }
        SqlConnection con=null ;
        String sConstring=null;
       
        private void menuExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close(); 
        }
       
        private void mnuDisConnect_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                sConstring = "Data Source =" + Host.Text.Trim() + ";Initial Catalog=" + catalogDb.Text.Trim() + ";Integrated Security=SSPI;";
                con = new SqlConnection(sConstring);
                con.Open();
                SqlCommand cmd = new SqlCommand("sp_ValidateSqlSyntax", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter Squery = new SqlParameter("@CustomQuery", SqlDbType.NVarChar);
                SqlParameter Status = new SqlParameter("@StatusMsg", SqlDbType.VarChar);
                FileInfo file = new FileInfo(txtqueryval.Text.Trim());
                string script = file.OpenText().ReadToEnd();
                Squery.Direction = ParameterDirection.Input;
                Status.Direction = ParameterDirection.Output;
                Status.Size = 100000;
                Squery.Value = script.ToString().Trim();
                Status.Value = "@StatusMsg";
                cmd.Parameters.Add(Squery);
                cmd.Parameters.Add(Status);
                object reader = cmd.ExecuteScalar();
                if (reader != null)
                {
                    txtError.Text = reader.ToString().Trim();
                  
                }
                else
                {
                    txtError.Text = "Success";
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "Sql Sytax Reviewer", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {

                FolderBrowserDialog f = new FolderBrowserDialog();
                OpenFileDialog f1 = new OpenFileDialog();
                f1.FileName = "";
                f1.Filter = "SQL  files (*.sql)|*.sql|All files (*.*)|*.*";
                f1.Filter = "SQL  files (*.sql)|*.sql";
                f1.Filter = "Allfiles|*.sql";

                //var dialog = new System.Windows.Forms.FolderBrowserDialog();
                //System.Windows.Forms.DialogResult result = dialog.ShowDialog();

                if (f1.ShowDialog() == System.Windows.Forms.DialogResult.OK  ) // selecting directory
                {
                   txtqueryval.Text = f1.FileName;
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "Image Resizer Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 
        }
        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
    
        }
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
        }
        private void catalogDb_TextChanged(object sender, TextChangedEventArgs e)
        {
        }
    }
}

 
    
     
-------------------------------------------------------------------------------------------------------------- 
 ----Developed By           : Manoj.B. 
 ----Date                   :14-08-2012  
 ----Using  Project         : Sql Syntax Validator   
 ----Usage                  :Can be used to test the format of the response without actually running the query.    
-------------------------------------------------------------------------------------------------------------- 
 
CREATE     PROC sp_ValidateSqlSyntax(         
@CustomQuery AS nVARCHAR(MAX),         
@StatusMsg AS nVARCHAR(MAX) OUTPUT         
)         
AS         
BEGIN         
BEGIN TRY         
IF @CustomQuery IS NOT NULL BEGIN          
Declare  @stat  as varchar(max)         
Declare  @error_number  as varchar(max)       
Declare  @error_severity  as varchar(max)       
Declare   @error_state  as varchar(max)       
Declare   @error_MSG  as varchar(max)       
Declare   @error_Line  as varchar(max)   
       
SET @CustomQuery =  'SET NOEXEC ON; ' + @CustomQuery + ' ; SET NOEXEC OFF;'         
SET @CustomQuery =  'SET FMTONLY ON; ' + @CustomQuery + ' ; SET FMTONLY  OFF;'         
END         
EXECUTE SP_EXECUTESQL @CustomQuery         
SET @StatusMsg = 'Success'         
END TRY         
--BEGIN CATCH         
----print ERROR_MESSAGE()         
--SELECT    ERROR_MESSAGE()       
--EXEC error_handling_proc        
--END CATCH         
--END       
       
       
BEGIN CATCH           
print Error_message()       
print ERROR_NUMBER()       
print ERROR_SEVERITY()       
print ERROR_STATE()       
print Error_line()         
       
select  Error_message() as msg       
select ERROR_NUMBER() as number       
select ERROR_SEVERITY() as sever       
select ERROR_STATE() as state        
select Error_line() as line        
       
       
SELECT @error_number = ERROR_NUMBER(),    @error_severity = ERROR_SEVERITY(),    @error_state = ERROR_STATE()   ,@error_MSG=error_message(),@error_Line=error_Line()        
IF @error_number = 547         
BEGIN     PRINT 'the value specified for building_id is out of range'           
END          
ELSE -- some other "untrapped" error has occured            
BEGIN  RAISERROR(@error_number, @error_severity, @error_state,@error_MSG,@error_Line) WITH LOG           
END         
END CATCH       
End  

Comments