Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
This is what I have done

C#
String scriptAndStub;

scriptAndStub =
	"DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)\n" +
	"SET @SA_CONSOLE_HOSTNAME = @HOSTNAME@\n";
scriptAndStub += script;

executeScript.CommandText = scriptAndStub;
executeScript.Parameters.AddWithValue("@HOSTNAME@", Environment.MachineName);



I tried having breakpoints and check where the error was caught

C#
private void btnSyntaxCheck_Click(object sender, EventArgs e)
        {
            try
            {
                tsStatus.Text = "Checking Syntax...";
                LogMessage(DA_Base.Constants.ERROR_LEVEL_DEBUG, "SQLScripting::SyntaxCheck", "Query: " + rtbScript.Text);
                using (SqlCommand myCommand = new SqlCommand(string.Empty, Connection))
                {
                   foreach (string script in Regex.Split(rtbScript.Text, "^GO\r?$", RegexOptions.Multiline | RegexOptions.IgnoreCase))
                      {
                           if (!String.IsNullOrEmpty(script))
                           {
                             myCommand.CommandText = "SET PARSEONLY ON\n" + script;
                             myCommand.ExecuteNonQuery();
                            }
                       }
                 }
                 tsStatus.Text = "Checking Syntax Complete. No errors reported.";
             }
             catch (Exception exc)\\error has been caught here....
             {
                tsStatus.Text = exc.Message;
                MessageBox.Show(exc.Message, "SQL Syntax Check");
             }
         }
   }
}
Posted
Updated 24-Apr-13 3:57am
v3
Comments
CHill60 24-Apr-13 10:01am    
Have you put a 'GO' in there? E.g.
DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)
GO
SET @SA_CONSOLE_HOSTNAME = 'whatever'
arsene 789 24-Apr-13 11:04am    
visual studio is not letting me add "GO", am new to this if you can give a detail explanation it would be great..

1 solution

MADE THESE CHANGES WORKING FINE

String scriptAndStub;

scriptAndStub =

"DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)\n"; +

"SET @SA_CONSOLE_HOSTNAME = @HOSTNAME@\n";
executeScript.Parameters.Clear();
scriptAndStub += script;

executeScript.CommandText = scriptAndStub;
executeScript.Parameters.AddWithValue("@HOSTNAME@", Environment.MachineName);


C#
private void btnSyntaxCheck_Click(object sender, EventArgs e)
        {

            try
            {
                tsStatus.Text = "Checking Syntax...";

                LogMessage(DA_Base.Constants.ERROR_LEVEL_DEBUG, "SQLScripting::SyntaxCheck", "Query: " + rtbScript.Text);

                String scriptAndStub;

                scriptAndStub = "DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)\n";

                using (SqlCommand myCommand = new SqlCommand(string.Empty, Connection))
                {
                    foreach (string script in Regex.Split(rtbScript.Text, "^GO\r?$", RegexOptions.Multiline | RegexOptions.IgnoreCase))
                    {
                        if (!String.IsNullOrEmpty(script))
                        {
                            myCommand.CommandText = "SET PARSEONLY ON\n" +scriptAndStub + script;
                            myCommand.ExecuteNonQuery();
                            //using (SqlDataAdapter adapter = new SqlDataAdapter())
                            //{
                            //    adapter.SelectCommand = myCommand;
                            //    using (DataTable table = new DataTable())
                            //    {
                            //        table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                            //        adapter.FillSchema(table, SchemaType.Source);
                            //    }
                            //}
                        }
                    }
                }

                tsStatus.Text = "Checking Syntax Complete. No errors reported.";
            }
            catch (Exception exc)
            {
                tsStatus.Text = exc.Message;
                MessageBox.Show(exc.Message, "SQL Syntax Check");
            }
        }



    }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-May-13 13:22pm    
Please don't post non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.
—SA
arsene 789 1-May-13 13:35pm    
Thanks for replying

I made the following changes and the code worked fine
The variable was storing previous parameters so by clearing them solved one part of the error
executeScript.Parameters.Clear();

And then by declaring the variable in the main function has cleared another error which showed variable was not declared.

String scriptAndStub;
scriptAndStub = "DECLARE @SA_CONSOLE_HOSTNAME VARCHAR(256)\n";

Thanks!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900