473,385 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Ad Hoc Query in C#

Ok...here we go...

I am trying to manually run an SQL server back up job from c#. Here is a
snippet of my code to begin the process:

string strSQL = "USE msdb " +
"\n if exists(select * from sysjobs_view where name = 'TempBackupJob') " +
"\n EXEC sp_delete_job @job_name = 'TempBackupJob' " +
"\n GO";
newDB.Command(strSQL);

This command exceptions with an "Incorrect syntax near 'GO'".
Now, when I put the EXACT same query into Query Analyzer like this:

USE msdb
if exists(select * from sysjobs_view where name = 'TempBackupJob')
EXEC sp_delete_job @job_name = 'TempBackupJob'
GO

the command executes without any errors.

Is there something I'm missing with newline characters? I also tried using
\r\n instead of just \n to separate the lines...that didn't work either.

Any help would be greatly appreciated.
--
Thanks,

John Scott.
Nov 3 '05 #1
12 2918
take out the carriage returns and line breaks

The /+/'s will let you break the lines in your c# code

Use an @ in front of the first " (double quote)

string strSQL = @"USE msdb " +
" if exists(select * from sysjobs_view where name = 'TempBackupJob') " +
" EXEC sp_delete_job @job_name = 'TempBackupJob' " +
" GO";
John Scott wrote:
Ok...here we go...

I am trying to manually run an SQL server back up job from c#. Here is a
snippet of my code to begin the process:

string strSQL = "USE msdb " +
"\n if exists(select * from sysjobs_view where name = 'TempBackupJob') " +
"\n EXEC sp_delete_job @job_name = 'TempBackupJob' " +
"\n GO";
newDB.Command(strSQL);

This command exceptions with an "Incorrect syntax near 'GO'".
Now, when I put the EXACT same query into Query Analyzer like this:

USE msdb
if exists(select * from sysjobs_view where name = 'TempBackupJob')
EXEC sp_delete_job @job_name = 'TempBackupJob'
GO

the command executes without any errors.

Is there something I'm missing with newline characters? I also tried using
\r\n instead of just \n to separate the lines...that didn't work either.

Any help would be greatly appreciated.

Nov 3 '05 #2
Why do you need the \n and \r ? It is not like you have to print out the sql
string to make it readable. You just need to execute it and considering that
it is SQL all you need is good spacing between your SQL statements and
commands.

Try it with no \r and no \n and see what happens..

Good luck

Amadelle

"John Scott" wrote:
Ok...here we go...

I am trying to manually run an SQL server back up job from c#. Here is a
snippet of my code to begin the process:

string strSQL = "USE msdb " +
"\n if exists(select * from sysjobs_view where name = 'TempBackupJob') " +
"\n EXEC sp_delete_job @job_name = 'TempBackupJob' " +
"\n GO";
newDB.Command(strSQL);

This command exceptions with an "Incorrect syntax near 'GO'".
Now, when I put the EXACT same query into Query Analyzer like this:

USE msdb
if exists(select * from sysjobs_view where name = 'TempBackupJob')
EXEC sp_delete_job @job_name = 'TempBackupJob'
GO

the command executes without any errors.

Is there something I'm missing with newline characters? I also tried using
\r\n instead of just \n to separate the lines...that didn't work either.

Any help would be greatly appreciated.
--
Thanks,

John Scott.

Nov 3 '05 #3
When I take out the \r\n the command exceptions too.( Incorrect syntax near
'GO')
I also tested something in Query Anaylzer..if the SQL statment is all on one
line...it fails...if everything else EXCEPT GO is on the same line...the
query runs successfully. So...I am under the assumption that GO has to be on
its own line when it is evaluated. That's why I was wondering about line
breaks.

Got anything else ??

--
Thanks,

John Scott.
"Amadelle" wrote:
Why do you need the \n and \r ? It is not like you have to print out the sql
string to make it readable. You just need to execute it and considering that
it is SQL all you need is good spacing between your SQL statements and
commands.

Try it with no \r and no \n and see what happens..

Good luck

Amadelle

"John Scott" wrote:
Ok...here we go...

I am trying to manually run an SQL server back up job from c#. Here is a
snippet of my code to begin the process:

string strSQL = "USE msdb " +
"\n if exists(select * from sysjobs_view where name = 'TempBackupJob') " +
"\n EXEC sp_delete_job @job_name = 'TempBackupJob' " +
"\n GO";
newDB.Command(strSQL);

This command exceptions with an "Incorrect syntax near 'GO'".
Now, when I put the EXACT same query into Query Analyzer like this:

USE msdb
if exists(select * from sysjobs_view where name = 'TempBackupJob')
EXEC sp_delete_job @job_name = 'TempBackupJob'
GO

the command executes without any errors.

Is there something I'm missing with newline characters? I also tried using
\r\n instead of just \n to separate the lines...that didn't work either.

Any help would be greatly appreciated.
--
Thanks,

John Scott.

Nov 3 '05 #4
Thanks for the feedback...I took the carriage returns and line breaks out and
added the @ at the beginning of the string before the parenthesis...just like
your example..but that command failed too.

I also replied to Amadelle saying that when the command I'm trying to run is
all on one line in Quyery Anaylzer...it fails there as well. But if the
command is all on one line except for 'GO' it runs successfully. So if GO is
on its own line the command is good.

Any other ideas??
--
Thanks,

John Scott.
"John A. Bailo" wrote:
take out the carriage returns and line breaks

The /+/'s will let you break the lines in your c# code

Use an @ in front of the first " (double quote)

string strSQL = @"USE msdb " +
" if exists(select * from sysjobs_view where name = 'TempBackupJob') " +
" EXEC sp_delete_job @job_name = 'TempBackupJob' " +
" GO";
John Scott wrote:
Ok...here we go...

I am trying to manually run an SQL server back up job from c#. Here is a
snippet of my code to begin the process:

string strSQL = "USE msdb " +
"\n if exists(select * from sysjobs_view where name = 'TempBackupJob') " +
"\n EXEC sp_delete_job @job_name = 'TempBackupJob' " +
"\n GO";
newDB.Command(strSQL);

This command exceptions with an "Incorrect syntax near 'GO'".
Now, when I put the EXACT same query into Query Analyzer like this:

USE msdb
if exists(select * from sysjobs_view where name = 'TempBackupJob')
EXEC sp_delete_job @job_name = 'TempBackupJob'
GO

the command executes without any errors.

Is there something I'm missing with newline characters? I also tried using
\r\n instead of just \n to separate the lines...that didn't work either.

Any help would be greatly appreciated.

Nov 3 '05 #5
Take out "GO". "GO" will not work through ADO.

HTH, Jakob.

--
http://www.dotninjas.dk
http://www.powerbytes.dk
"John Scott" wrote:
When I take out the \r\n the command exceptions too.( Incorrect syntax near
'GO')
I also tested something in Query Anaylzer..if the SQL statment is all on one
line...it fails...if everything else EXCEPT GO is on the same line...the
query runs successfully. So...I am under the assumption that GO has to be on
its own line when it is evaluated. That's why I was wondering about line
breaks.

Got anything else ??

--
Thanks,

John Scott.
"Amadelle" wrote:
Why do you need the \n and \r ? It is not like you have to print out the sql
string to make it readable. You just need to execute it and considering that
it is SQL all you need is good spacing between your SQL statements and
commands.

Try it with no \r and no \n and see what happens..

Good luck

Amadelle

"John Scott" wrote:
Ok...here we go...

I am trying to manually run an SQL server back up job from c#. Here is a
snippet of my code to begin the process:

string strSQL = "USE msdb " +
"\n if exists(select * from sysjobs_view where name = 'TempBackupJob') " +
"\n EXEC sp_delete_job @job_name = 'TempBackupJob' " +
"\n GO";
newDB.Command(strSQL);

This command exceptions with an "Incorrect syntax near 'GO'".
Now, when I put the EXACT same query into Query Analyzer like this:

USE msdb
if exists(select * from sysjobs_view where name = 'TempBackupJob')
EXEC sp_delete_job @job_name = 'TempBackupJob'
GO

the command executes without any errors.

Is there something I'm missing with newline characters? I also tried using
\r\n instead of just \n to separate the lines...that didn't work either.

Any help would be greatly appreciated.
--
Thanks,

John Scott.

Nov 4 '05 #6
Rather than using SQL like this just build a stored procedure that contains
the logic. Then call the stored procedure from your code. Always makes for
cleaner and safe code this way.

"John Scott" <jo*******@despammed.com> wrote in message
news:36**********************************@microsof t.com...
Ok...here we go...

I am trying to manually run an SQL server back up job from c#. Here is a
snippet of my code to begin the process:

string strSQL = "USE msdb " +
"\n if exists(select * from sysjobs_view where name = 'TempBackupJob') " +
"\n EXEC sp_delete_job @job_name = 'TempBackupJob' " +
"\n GO";
newDB.Command(strSQL);

This command exceptions with an "Incorrect syntax near 'GO'".
Now, when I put the EXACT same query into Query Analyzer like this:

USE msdb
if exists(select * from sysjobs_view where name = 'TempBackupJob')
EXEC sp_delete_job @job_name = 'TempBackupJob'
GO

the command executes without any errors.

Is there something I'm missing with newline characters? I also tried using \r\n instead of just \n to separate the lines...that didn't work either.

Any help would be greatly appreciated.
--
Thanks,

John Scott.

Nov 4 '05 #7

He could do that, or just parameterize his string sql query.

You should never put values in quotes in a string.

Use @vars and replace them with sql params, just like in a stored procedure.

Putting logic like this in a stored procedure isn't alway to my liking
(although it seems to be Gospel in the MS camp).

It's perfectly reasonable to use queries in ado, and it gives the
programmer far more control than having to go back and forth to sprocs
and code.
Jim Underwood wrote:
Rather than using SQL like this just build a stored procedure that contains
the logic. Then call the stored procedure from your code. Always makes for
cleaner and safe code this way.

"John Scott" <jo*******@despammed.com> wrote in message
news:36**********************************@microsof t.com...
Ok...here we go...

I am trying to manually run an SQL server back up job from c#. Here is a
snippet of my code to begin the process:

string strSQL = "USE msdb " +
"\n if exists(select * from sysjobs_view where name = 'TempBackupJob') " +
"\n EXEC sp_delete_job @job_name = 'TempBackupJob' " +
"\n GO";
newDB.Command(strSQL);

This command exceptions with an "Incorrect syntax near 'GO'".
Now, when I put the EXACT same query into Query Analyzer like this:

USE msdb
if exists(select * from sysjobs_view where name = 'TempBackupJob')
EXEC sp_delete_job @job_name = 'TempBackupJob'
GO

the command executes without any errors.

Is there something I'm missing with newline characters? I also tried


using
\r\n instead of just \n to separate the lines...that didn't work either.

Any help would be greatly appreciated.
--
Thanks,

John Scott.


Nov 4 '05 #8
I suppose it is a little different for me, since I have always had access to
the database as well as the application code.

I always like to put as much logic in the database as possible, simply
because I can change the stored procedure without needing to repackage and
deploy the application. It also makes for much cleaner code, although the
tradeoff is the programmer cannot see what is going on in the database.
This last piece can be either a pro or a con, depending on how knowledgeable
and involved the programmer is with the database code.

The other benefit is that it is much simpler to debug a single stored
procedure call than multiple lines of concatenated text.

"John A. Bailo" <ja*****@texeme.com> wrote in message
news:_s********************@speakeasy.net...

He could do that, or just parameterize his string sql query.

You should never put values in quotes in a string.

Use @vars and replace them with sql params, just like in a stored procedure.
Putting logic like this in a stored procedure isn't alway to my liking
(although it seems to be Gospel in the MS camp).

It's perfectly reasonable to use queries in ado, and it gives the
programmer far more control than having to go back and forth to sprocs
and code.
Jim Underwood wrote:
Rather than using SQL like this just build a stored procedure that contains the logic. Then call the stored procedure from your code. Always makes for cleaner and safe code this way.

"John Scott" <jo*******@despammed.com> wrote in message
news:36**********************************@microsof t.com...
Ok...here we go...

I am trying to manually run an SQL server back up job from c#. Here is asnippet of my code to begin the process:

string strSQL = "USE msdb " +
"\n if exists(select * from sysjobs_view where name = 'TempBackupJob') " +"\n EXEC sp_delete_job @job_name = 'TempBackupJob' " +
"\n GO";
newDB.Command(strSQL);

This command exceptions with an "Incorrect syntax near 'GO'".
Now, when I put the EXACT same query into Query Analyzer like this:

USE msdb
if exists(select * from sysjobs_view where name = 'TempBackupJob')
EXEC sp_delete_job @job_name = 'TempBackupJob'
GO

the command executes without any errors.

Is there something I'm missing with newline characters? I also tried


using
\r\n instead of just \n to separate the lines...that didn't work either.

Any help would be greatly appreciated.
--
Thanks,

John Scott.


Nov 4 '05 #9

You know -- that really rings true.

I wish -- or maybe I could invent through operator overloading -- a
better way to put in long multiline strings in c# code.

Maybe with an XML comment insert ?

So the code would look like
<DEFINE id="mysqlquery>
SELECT * FROM TABLE
WHERE FIELD1=@VALUE
ORDER BY @ARG1
</DEFINE>

and I could put that right in a method, and then say something like

MySqlCommand.CommandText = SpecialXMLFunction("mysqlquer");


Jim Underwood wrote:
The other benefit is that it is much simpler to debug a single stored
procedure call than multiple lines of concatenated text.

Nov 4 '05 #10
Being able to write readable SQL within the application code itself would be
nice, as you would be able to see everything in one place at the same time,
rather than going from the code to the database and back.
"John A. Bailo" <ja*****@texeme.com> wrote in message
news:43**************@texeme.com...

You know -- that really rings true.

I wish -- or maybe I could invent through operator overloading -- a
better way to put in long multiline strings in c# code.

Maybe with an XML comment insert ?

So the code would look like
<DEFINE id="mysqlquery>
SELECT * FROM TABLE
WHERE FIELD1=@VALUE
ORDER BY @ARG1
</DEFINE>

and I could put that right in a method, and then say something like

MySqlCommand.CommandText = SpecialXMLFunction("mysqlquer");


Jim Underwood wrote:
The other benefit is that it is much simpler to debug a single stored
procedure call than multiple lines of concatenated text.

Nov 4 '05 #11
I wonder if it can be treated as an "attribute".

Somehow it doesn't seem exactly a match, but clearly I can add in XML
attributes that are attached to classes and use Reflection to utilize them.

I'm going to have to review Reflectin/Attributes to see if it can be done.
Jim Underwood wrote:
Being able to write readable SQL within the application code itself would be
nice, as you would be able to see everything in one place at the same time,
rather than going from the code to the database and back.
"John A. Bailo" <ja*****@texeme.com> wrote in message
news:43**************@texeme.com...
You know -- that really rings true.

I wish -- or maybe I could invent through operator overloading -- a
better way to put in long multiline strings in c# code.

Maybe with an XML comment insert ?

So the code would look like
<DEFINE id="mysqlquery>
SELECT * FROM TABLE
WHERE FIELD1=@VALUE
ORDER BY @ARG1
</DEFINE>

and I could put that right in a method, and then say something like

MySqlCommand.CommandText = SpecialXMLFunction("mysqlquer");


Jim Underwood wrote:

The other benefit is that it is much simpler to debug a single stored
procedure call than multiple lines of concatenated text.


Nov 4 '05 #12
John A. Bailo <ja*****@texeme.com> wrote:
I wonder if it can be treated as an "attribute".

Somehow it doesn't seem exactly a match, but clearly I can add in XML
attributes that are attached to classes and use Reflection to utilize them.

I'm going to have to review Reflectin/Attributes to see if it can be done.


I think what you're really after is NHibernate...
http://www.nhibernate.org

(You might also want to look at DLINQ...)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 5 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
3
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
4
by: Diamondback | last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has descriptive information about the widgets while the VERSIONS table contains IDs relating to different iterations of those widgets...
14
by: Dave Thomas | last post by:
If I have a table set up like this: Name | VARCHAR Email | VARCHAR Age | TINYINT | NULL (Default: NULL) And I want the user to enter his or her name, email, and age - but AGE is optional. ...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
4
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can I run a query of a query datasheet. I want to use more that one criteria and can not get that query to work. I thought I...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.