473,513 Members | 2,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2927
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
3413
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
2477
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
3109
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
5372
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
2133
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
3853
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
3485
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
4816
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
3113
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
4373
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
7257
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
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7535
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...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.