Works in Query Analyzer, why not in SqlCommand.ExecuteQuery() ?

  • Thread starter Simon Tamman {Uchiha Jax}
  • Start date
S

Simon Tamman {Uchiha Jax}

Here is the code:

SqlCommand nonQuery = myConnection.CreateCommand();
nonQuery.CommandText = command.Command;
result.Returned = nonQuery.ExecuteNonQuery(); /// exception occurs here
result.Success = true;

The command property of the command object (which is one of my custom
objects) is this:

USE Master
GO
CREATE DATABASE CynoNUnitTestDB
GO
USE CynoNUnitTestDB
GO
CREATE TABLE CynoUser........ (the rest is omitted for the sake of brevity)

I get the following exception:

Line 1: Incorrect syntax near 'GO'.
Line 1: Incorrect syntax near 'GO'.
Could not locate entry in sysdatabases for database 'CynoNUnitTestDB'. No
entry found with that name. Make sure that the name is entered correctly.

The connection string is this: Data Source=JAX;Initial
Catalog=Master;Integrated Security=SSPI;


Why is this a-ok with Query Analyzer but no good with the SqlCommand object?

Please assist.

Kind Regards

Jax
 
A

AMDRIT

From books online:

GO is not a Transact-SQL statement; it is a command recognized by the osql
and isql utilities and SQL Query Analyzer.

SQL Server utilities interpret GO as a signal that they should send the
current batch of Transact-SQL statements to SQL Server. The current batch of
statements is composed of all statements entered since the last GO, or since
the start of the ad hoc session or script if this is the first GO. SQL Query
Analyzer and the osql and isql command prompt utilities implement GO
differently. For more information, see osql Utility, isql Utility, and SQL
Query Analyzer.



In short "Go" is invalid in ADO.Net.

possible work around, stored procedure, passing in what you want it to do.

another possible workaround, multiple calls to ExecuteNonQuery





"Simon Tamman {Uchiha Jax}"
 
S

Simon Tamman {Uchiha Jax}

Ahhhh, I now see.
Thank you very much. I have now split the commands up and it works fine now.
Problem solved! :)
 
M

Mark Ashton

Sql Server doesn't know what "GO" is.
QueryAnalyzer parses the input text and sends sections between "GO" as a
separate command.

--
This posting is provided "AS IS", with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.

"Simon Tamman {Uchiha Jax}"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top